Php 特定树的递归函数

Php 特定树的递归函数,php,arrays,recursion,Php,Arrays,Recursion,我正在努力解决一个非常糟糕的递归函数。我有以下数组 $structure_array = array( 0 => array( 'name' => 'Shop All', 'children' => array( // this has 2 children 0 => array( 'name' => 'RC Something', 'children' => array( //

我正在努力解决一个非常糟糕的递归函数。我有以下数组

$structure_array = array(

0 => array(
    'name' => 'Shop All',
    'children' => array( // this has 2 children
        0 => array(
            'name' => 'RC Something',
            'children' => array( // this only has 1 child
                0 => array(
                    'name' => 'Boats'
                )
            )
        ),
        1 => array(
            'name' => 'Something else'
        )
    )
)
);
我们需要从中解脱出来

// 0 => array(Shop ALl)
// 1 => array(Shop ALl, RC Something)
// 2 => array(Shop ALl, RC Something, 'Boats')
// 3 => array(Shop ALl, Something Else)
我想不出一个合适的方法来做这件事。原始代码被破坏,并且正在使用的递归函数返回了错误的结果。有什么想法吗

提前谢谢

编辑:这是原始的递归:

private function getNestedCats($cats, $names, &$results, $id_lang)
{
    foreach ($cats as $cat)
    {

    }

    // try to return a string with all subcats

    foreach ($cats as $cat)
    {
        if (isset($cat['children']) && is_array($cat['children']) && count($cat['children']) > 0)
        {
            if ($cat['is_root_category'] == 0)
                $names[] = $cat['name'];

            $this->getNestedCats($cat['children'], $names, $results, $id_lang);
        }
        else
        {
            if ($cat['is_root_category'] == 0)
                $names[] = $cat['name'];

            $results[] = $names;
            // array_pop($names);
        }
    }
}
我去掉了我的第一个测试,但基本上我去掉了引用参数结果,在childrenif块中,循环遍历children并将第一个键附加到$names。然后将$names添加到$results,并最终返回该结果。 它不起作用,因为我只得到像商店所有,商店所有。2次全部购物,在最终阵列中。

好的,那怎么办

global $solution;
$solution = array();
function fillSolution($key, $value, $prefix) {
    global $solution;
    if(is_string($key) && $key == "name") {
        if(empty(trim($prefix))) {
            $prefix .= "$value";
        } else {
            $prefix .= ", $value";           
        }
        array_push($solution, $prefix);
        return $prefix;
    } else { // $key is numeric or children and value is an array
        $p = $prefix;
        foreach($value as $k => $v) {
            $prefix = fillSolution($k, $v, $prefix);
        }
        return $p;
    }
}
fillSolution(0, $structure_array[0], '');

print_r($solution);

for($i=0; $i < count($solution); $i++) {
    echo "// $i => array($solution[$i])\n";
    // use </br> instead of \n for printing new line in html
}

到目前为止你尝试了什么?听起来像是我们的家庭作业=)你期望的结果是什么?您只想在屏幕上打印
0=>array(Shop ALl)
等,还是希望以后可以使用数组?我添加了该函数。我需要一个阵列,我可以在以后使用:)的作品像一个魅力,非常感谢!空检查返回了一个错误,但在任何情况下,我都将前缀转换为数组,因为这正是我所需要的。你救了我一天!
Array                                                                           
( 
    [1] => Shop All, RC Something                                               
    [2] => Shop All, RC Something, Boats                                        
    [3] => Shop All, Something else                                             
)                                                                               
// 0 => array(Shop All)                                                         
// 1 => array(Shop All, RC Something)                                           
// 2 => array(Shop All, RC Something, Boats)                                    
// 3 => array(Shop All, Something else)