PHP递归,返回的位置

i've got this little snippet to get the ids from a multidimensional array.

<?php

$array = array(
    array(
        'id' => 1,
        'children' => array(
            array(
                'id' => 12,
                'children' => array()
            ),
            array(
                'id' => 13,
                'children' => array(
                    array(
                        'id' => 112,
                        'children' => array()
                    )
                )
            )
        )
    ),
    array(
        'id' => 140,
        'children' => array(
            array(
                'id' => 144,
                'children' => array(
                    array(
                        'id' => 101,
                        'children' => array()
                    )
                )
            )
        )
    ),
    array(
        'id' => 32,
        'children' => array(
            array(
                'id' => 223,
                'children' => array()
            )
        )
    ),
    array(
        'id' => 40,
        'children' => array()
    )
);



function get_ids( $array, $result )
{
    foreach( $array as $a )
    {
        $result[] = $a['id'] . "
";

        if ( count( $a['children'] ) )
        {           
            return get_ids( $a['children'], $result );
        }
    }   

    return $result;


}

print_r( get_ids( $array, array() ) );

This code outputs the first array ids like this:

Array
(
    [0] => 1

    [1] => 12

    [2] => 13

    [3] => 112

)

If i instead of $result[] put echo and removes the return on return get_ids row it echoes out every id as intended, the problem is i'd like to have them in a nice looking array with the ids.

I think i've messed up the return statements but i have no idea.

The first return returns only the ids of the child; you lose the id of the parent. You need both.

So, replace this:

return get_ids( $a['children'], $result );

With this:

$result = array_merge( $result, get_ids( $a['children'] ));