PHP: Modifying array recursively?

Posted by Industrial on Stack Overflow See other posts from Stack Overflow or by Industrial
Published on 2010-06-08T16:34:02Z Indexed on 2010/06/08 16:52 UTC
Read the original article Hit count: 313

Filed under:
|
|
|

Hi everybody,

I have tried to make a function that iterates through the following array to flatten it and add parent id to children, where applicable. I just can't make it work, so I hope that anyone here has an idea of what to do:

Here's the starting point:

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

    [1] => Array (
            [id] => 2
            [children] => Array (
                    [0] => Array (
                            [id] => 3
                    )
             )
        )
)

The expected result :

Array (

    [0] => array ( 
        [id] => 1
    )

    [1] => array ( 
        [id] => 2
    )

    [2] => array ( 
        [id] => 3,
        [parent] => 2
    ) 

)

Hope that anyone can point me in the right direction. Thanks a lot!

© Stack Overflow or respective owner

Related posts about php

Related posts about php5