views:

57

answers:

1

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
            [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>");
+4  A: 

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
        )

)
Oli
Hi Oli! Thanks a lot for your help! Unfortunately it only returns an empty array :(Do you know why?
Industrial
`$in->children`? `$in->parent`?
webbiedave
Again, forgot what language and object I was dealing with. Try this.
Oli
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
Industrial
Yeah there were a few silly errors in there still. Fixed it and tested it.
Oli
Hi Oli. Many thanks for taking your time to helping me out. I am really happy for you doing this! However, it's now says "Message: Undefined index: children" and " Message: Invalid argument supplied for foreach()" when trying to test run your snippet :)
Industrial
I was able to think it out, finally. Had to put a return in the end and initiate each call to deJigg in the foreach loop. Now it's working like a charm. Thanks a lot!
Industrial
Updated my original post with the modified solution, Many thanks to Oli the man!
Industrial