特定树的递归函数

时间:2015-09-04 10:56:31

标签: php arrays recursion

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

$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);
        }
    }
}

我摆脱了我的第一次测试,但基本上我摆脱了参考参数结果,并且在子节点中,如果阻塞,循环通过子节点并将第一个键附加到$ names。然后将$ name添加到$ results并最终返回。 它没有工作,因为我只喜欢购物,购物。 2次全部购买,在最后一个阵列中。

1 个答案:

答案 0 :(得分:0)

好的,那又怎么样?

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
}

这将打印:

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)