3

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
            [children] => 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!

Solution (Thanks to Oli!):

$output = array();

        function dejigg($in) {
            global $output;

            if (!isset($in['children'])) {
                $in['children'] = array();
            }
            $kids = $in['children'] or array();
            unset($in['children']);
            if (!isset($in['parent'])) {
                $in['parent'] = 0; // Not neccessary but makes the top node's parent 0.
            }
            $output[] = $in;

            foreach ($kids as $child) {
                $child['parent'] = $in['id'];
                dejigg($child); // recurse
            }

            return $output;
        }

        foreach ($array as $parent) {
            $output[] = dejigg($parent);
        }

        $array = $output;
        print("<pre>".print_r($array,true)."</pre>");
1
  • You want the third element in the expected result to be 2, not 3, correct? Just a typo? Commented Jun 8, 2010 at 16:38

1 Answer 1

4

I've tested it this time. This does work!

$input = array( array('id' => 1, 'children'=>array( array('id'=>2, 'children'=>array( array('id'=>3) ) ) ) )  );
$output = [];

function dejigg($in) {
    global $output;

    $kids = $in['children'] or array();
    unset($in['children']);
    $output[] = $in;

    foreach ($kids as $child) {
        $child['parent'] = $in['id'];
        dejigg($child); // recurse
    }
}

foreach ($input as $parent)
    dejigg($parent);

print_r($output);

And it returns:

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

    [1] => Array
        (
            [id] => 2
            [parent] => 1
        )

    [2] => Array
        (
            [id] => 3
            [parent] => 2
        )

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

8 Comments

Hi Oli! Thanks a lot for your help! Unfortunately it only returns an empty array :( Do you know why?
$in->children? $in->parent?
Again, forgot what language and object I was dealing with. Try this.
Hi again Oli, thanks for updating, but it's still not working. I believe it's the unset that's causing the problems, but I cant clear it out by myself
Yeah there were a few silly errors in there still. Fixed it and tested it.
|

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.