PHP获取数组键根

I got the following structure :

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

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

                    [2] => Array
                        (
                            [id] => 8
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 10
                                        )

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

                                )

                        )

                    [3] => Array
                        (
                            [id] => 6
                        )

                    [4] => Array
                        (
                            [id] => 3
                        )

                    [5] => Array
                        (
                            [id] => 2
                        )

                    [6] => Array
                        (
                            [id] => 9
                        )

                )

        )

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

)

What I would like to know is a key's root structure. In other words.

We have ID number 10. And I would like to know the root structure (at least. that's how I would describe it)

The result should be :

$result = array(
                    0   =>  1,
                    1   =>  8
                );

But I have no clue how to achieve this. Nor how to start. The only thing I could come up with is creating several sub arrays with all child's in it and afterwards run through the arrays again and check if the ID is available. But in case of a complex structure. This will be almost undoable in terms of speed.

I hope anyone knows a solution to this issue.

nice that this post gets downvoted because people think it is too simple. but I am still looking for the answer. a recursive function hasn't worked for me.

///########-------------------------------------------------------------
///########-------------------------------------------------------------
///######## FUNCTION TO GET THE PAGE ORDER
///########-------------------------------------------------------------
///########-------------------------------------------------------------
private function GetPageLocation($PageArray, $PreFix = array()){
    ///######## RETURN THE COLLECTED DATA
    foreach($PageArray as $key => $value){
        ///######## IF THE PAGE HAS BEEN SET
        if(isset($value['id']) === true){
            echo PHP_EOL.$value['id'];
            ///######## IF THE ID IS NOT THE CURRENT ID
            if($value['id'] != $this->PageID){
                ///######## ADD THE PREFIX
                $PreFix[] = $value['id'];
            }
            ///######## IF THE ID IS THE CURRENT ID
            else{
                exit('sdd');
                ///######## HALT THE LOOP
                break;
            }
        }
        ///######## IF THE VALUE IS AN ARRAY
        if(isset($value['children']) === true){
            ///######## EXECUTE HIMSELF
            $PreFix = array_merge($PreFix, $this->GetPageLocation($value, $PreFix));
        }
        ///######## IF THE VALUE IS NO ARRAY
        else{

        }
    }
    ///########==================================================
    ///######## RETURN THE PREFIX
    ///########==================================================
    return($PreFix);
    ///########==================================================
}

A simple recursive solution:

<?php
// your array:
$arr = array(
    array(
        'id' => 1,
        'children' => array(
            array( 'id' => 5 ),
            array( 'id' => 7 ),
            array( 'id' => 8,
                   'children' => array(
                        array( 'id' => 10),
                        array( 'id' => 4 )
                    )
            ),
            array( 'id' => 6 ),
            array( 'id' => 3 ),
            array( 'id' => 2 ),
            array( 'id' => 9 )
        )
    ),
    array( 'id' => 13 )
);

// The recursive function which finds the path to the element having the specified $id
// through the $out parameter in the specified $arr.
function path_to_element($arr, $id, &$out) {
    foreach($arr as $el) {
        if($el['id']==$id) return true;
        else if(isset($el['children']) && is_array($el['children'])) {
            $out[] = $el['id'];
            if(path_to_element($el['children'], $id, $out)!==true) 
                array_pop($out);
            else return true;
        }
    }
    return false;
}

$out = array(); // The array will be filled with the path to the element
echo '<pre>';
path_to_element($arr, 10, $out);
print_r($out);
?>

The output will be:

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