S
S
semki0962016-05-17 13:35:10
PHP
semki096, 2016-05-17 13:35:10

How to convert an array of this kind?

[blocks] => Array
        (
            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [position] => 1
                        )

                )

            [2] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                             [position] => 2
                        )

                    [1] => Array
                        (
                            [id] => 3
                             [position] => 2
                        )
                )
        )

Question on this array. This is an array of blocks. The value [position] => 2 indicates that the block is in the second line. Accordingly, I output the array in a loop. The question is how can I pass the [position] value to the top, that is, transform the array to this form
[blocks] => Array
        (   
            [1] => Array
                (   [position] => 1
                    [0] => Array
                        (
                            [id] => 1
                            [position] => 1
                        )

                )

            [2] => Array
                (   [position] => 2
                    [0] => Array
                        (
                            [id] => 2
                             [position] => 2
                        )

                    [1] => Array
                        (
                            [id] => 3
                             [position] => 2
                        )
                )
        )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IceJOKER, 2016-05-17
@semki096

1st option - passing through the array to manually dig deep into the get position;
Option 2 - walk through the array and create a new array with a new structure.
3rd option - initially create the structure that you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question