I have an array and I want to add a dimension. I want to group it using the position column in the array
$view_rows = [];
Array
(
[row] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
[2] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
After adding the dimension (group by location), it should look like this:
Array
(
[rows] => Array
(
[1] => Array
(
[0] => Array
(
[position] => 1
[image_id] => 2809
[basename] => rj5ed2c90f609423.26673093.jpg
)
[1] => Array
(
[position] => 1
[image_id] => 2808
[basename] => rj5ed2c8eccc9c06.91700011.jpg
)
)
[2] => Array
(
[0] => Array
(
[position] => 2
[image_id] => 2807
[basename] => rj5ed2c77ef32b76.96579137.jpg
)
)
)
)
Is there a PHP function to do this? If not, I will use the good old looping method.
I did some research here ( Add Dimension in PHP array ), and I believe it can be achieve using array_map.
I've tried it, with no success (I'm close)
$new_array = array_map(function($x) {
return [$x[0]['position'] => $x];
}, $view_rows);