0

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);
0

2 Answers 2

1

Since you need to change the structure of the array, you can't use array_map, but you can use array_reduce to get the result you want:

$data['rows'] = array_reduce($data['rows'], function ($c, $a) {
    $c[$a['position']][] = $a;
    return $c;
}, array());

Output:

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
                        )    
                )    
        )    
)

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

2 Comments

If I don't want to change the structure, but assign the values to a new array, can I use array_map? I've adapted your solution to do just that (assign the values to a new array), but I was just wondering if I could do that with array_map (I've try with no success)
@Marco, you can just change the assignment, use $new_array = array_reduce(...) instead of $data['rows'] = array_reduce(...). You can't easily use array_map because array_map gives you a result with the same structure as the input.
1

@Nick answer works but the same thing can be achieved using a simple foreach loop easily so everyone can understand it.

$result = [];
foreach($data['rows'] as $row) {
    $result[$row['position']][] = $row;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.