views:

134

answers:

3

Hi,

  • What I want, is a function that searches through my array, and returns all the children to a specific node. What is the most appropriate way to do this? Will recursion be necessary in this case?

I have previously constructed a few quite complex functions that iterates with or without the help of recursion through multi-dimensional arrays and re-arranging them but this problem makes me completely stuck and I can't just get my head around it...

Here's my array:

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

        )

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

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

Thanks a lot,

UPDATE:
The output which I want to get. Sorry for the bad example, but I'll blame it on lack of knowledge on how to format the stuff I need to do :)

function getAllChildren($id) {
    // Psuedocode
    return $array;
}

getAllChildren(1); // Outputs the following:

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

    [3] => Array (
            [id] => 3
            [parent] => 2
        )   
)
+1  A: 
<?php
function searchItem($needle)
{
    foreach ($data as $key => $item)
    {
        if ($item['id'] == $needle)
        {
            return $key;
        }
    }
    return null;
}
?>
Sjoerd
Hi, thanks! Im afraid that you misunderstood me though. I need the children nodes to a specific "id"
Industrial
I don't understand. Can you supply an example?
Sjoerd
Updated original post. Thanks for your interest :)
Industrial
+1  A: 

Check out the array_walk_recursive() function in PHP:

http://www.php.net/manual/en/function.array-walk-recursive.php

Martin Bean
+4  A: 
$nodes = array( 1   => array (  'id'        => 1,
                                'parent'    => 0
                             ),
                2   => array ( 'id'         => 2,
                               'parent'     => 1
                             ),
                3   => array ( 'id'         => 3,
                               'parent'     => 2
                             )
                );


function searchItem($needle,$haystack) {
    $nodes = array();
    foreach ($haystack as $key => $item) {
        if ($item['parent'] == $needle) {
            $nodes[$key] = $item;
            $nodes = $nodes + searchItem($item['id'],$haystack);
        }
    }
    return $nodes;
}


$result = searchItem('1',$nodes);
echo '<pre>';
var_dump($result);
echo '</pre>';

Non-recursive version of the searchItem() function:

function searchItem($needle,$haystack) {
    $nodes = array();
    foreach ($haystack as $key => $item) {
        if (($item['parent'] == $needle) || array_key_exists($item['parent'],$nodes)) {
            $nodes[$key] = $item;
        }
    }
    return $nodes;
}

(assumes ordering of the parents/children, so a child node isn't included in the array unless the parent is already there)

Mark Baker
Hi mark! Thanks for your help! How would you modify it to be able to deal with finding multi-level children's?
Industrial
Industrial
Hi Mark, Thanks a lot for your update. It works like a charm!
Industrial
The second one doesn't work as intended, does it? I only get one result back.
Gordon
It produces the same results as the first (recursive) function using the same initial array as shown in my code, but won't return the correct results if a child is defined in the array before its parent
Mark Baker
@Industrial I've removed my post. While you *can* do it with SPL, it does overcomplicate things for your UseCase. Mark's solution is fine. *Keep it Simple*.
Gordon
Alright Gordon, thanksa a lot for your time. I will check out SPL in the future. Should be good to learn :)Big ups to Mark baker for the solution!
Industrial