使用递归从树中正确返回值

时间:2014-05-31 14:38:00

标签: php recursion tree

这是我的代码

https://eval.in/157357

如果您想看到树,可以使用json解析器。该树由Magento SOAP Api返回。 http://www.magentocommerce.com/api/soap/catalog/catalogCategory/catalog_category.tree.html

<?php
function getCategoryId($tree,$needle,&$val)
{
    if(!empty($tree["name"]) && $tree["name"] === $needle)
    {
        $val = $tree["category_id"];
        return $tree["category_id"];
    }
    else
    {   
        if(isset($tree["children"]["category_id"]))
        {
             getCategoryId($tree["children"],$needle,$val);
        }
        else
        {
            foreach ($tree["children"] as $child) {
               getCategoryId($child,$needle,$val);
            }
        }
    }
}

function main()
{

    $testjson = <<<EOL
{"category_id":"1","parent_id":"0","name":"Root Catalog","is_active":null,"position":"0","level":"0","children":[{"category_id":"2","parent_id":"1","name":"catroot","is_active":"1","position":"1","level":"1","children":[{"category_id":"3","parent_id":"2","name":"cat1","is_active":"1","position":"1","level":"2","children":[{"category_id":"7","parent_id":"3","name":"cat11","is_active":"0","position":"1","level":"3","children":[]},{"category_id":"8","parent_id":"3","name":"cat12","is_active":"0","position":"2","level":"3","children":[]}]},{"category_id":"9","parent_id":"2","name":"cat2","is_active":"0","position":"2","level":"2","children":[{"category_id":"11","parent_id":"9","name":"cat21","is_active":"0","position":"1","level":"3","children":[{"category_id":"12","parent_id":"11","name":"cat211","is_active":"0","position":"1","level":"4","children":[]},{"category_id":"13","parent_id":"11","name":"cat212","is_active":"0","position":"2","level":"4","children":[]}]}]},{"category_id":"10","parent_id":"2","name":"cat3","is_active":"0","position":"3","level":"2","children":[{"category_id":"14","parent_id":"10","name":"cat31","is_active":"0","position":"1","level":"3","children":[]},{"category_id":"15","parent_id":"10","name":"cat32","is_active":"0","position":"2","level":"3","children":[{"category_id":"16","parent_id":"15","name":"cat321","is_active":"0","position":"1","level":"4","children":[{"category_id":"17","parent_id":"16","name":"cat3211","is_active":"0","position":"1","level":"5","children":[]}]}]}]}]}]}
EOL;

    $result = (json_decode($testjson,true));

    $res = getCategoryId($result,"cat211",$val);
    var_dump($res);
    var_dump($val);

}

main();
?>

为什么我的函数getCategoryId返回NULL?我不想使用juste作为测试的引用$ val。

1 个答案:

答案 0 :(得分:1)

您需要从递归调用中返回结果:

<?php
    function getCategoryId($tree,$needle,&$val)
    {
        if(!empty($tree["name"]) && $tree["name"] === $needle)
        {
            //var_dump($tree);
            //var_dump($tree["category_id"]);
            $val = $tree["category_id"];
            return $tree["category_id"];
        }
        else
        {   
            if(isset($tree["children"]["category_id"]))
            {
                 return getCategoryId($tree["children"],$needle,$val);
            }
            else
            {
                foreach ($tree["children"] as $child) {
                   $return = getCategoryId($child,$needle,$val);
                   if($return){
                       return $return;
                   }
                }
            }
        }
    }

    function main()
    {

        $testjson = <<<EOL
{"category_id":"1","parent_id":"0","name":"Root Catalog","is_active":null,"position":"0","level":"0","children":[{"category_id":"2","parent_id":"1","name":"catroot","is_active":"1","position":"1","level":"1","children":[{"category_id":"3","parent_id":"2","name":"cat1","is_active":"1","position":"1","level":"2","children":[{"category_id":"7","parent_id":"3","name":"cat11","is_active":"0","position":"1","level":"3","children":[]},{"category_id":"8","parent_id":"3","name":"cat12","is_active":"0","position":"2","level":"3","children":[]}]},{"category_id":"9","parent_id":"2","name":"cat2","is_active":"0","position":"2","level":"2","children":[{"category_id":"11","parent_id":"9","name":"cat21","is_active":"0","position":"1","level":"3","children":[{"category_id":"12","parent_id":"11","name":"cat211","is_active":"0","position":"1","level":"4","children":[]},{"category_id":"13","parent_id":"11","name":"cat212","is_active":"0","position":"2","level":"4","children":[]}]}]},{"category_id":"10","parent_id":"2","name":"cat3","is_active":"0","position":"3","level":"2","children":[{"category_id":"14","parent_id":"10","name":"cat31","is_active":"0","position":"1","level":"3","children":[]},{"category_id":"15","parent_id":"10","name":"cat32","is_active":"0","position":"2","level":"3","children":[{"category_id":"16","parent_id":"15","name":"cat321","is_active":"0","position":"1","level":"4","children":[{"category_id":"17","parent_id":"16","name":"cat3211","is_active":"0","position":"1","level":"5","children":[]}]}]}]}]}]}
EOL;

        $result = (json_decode($testjson,true));

        $res = getCategoryId($result,"cat211",$val);
        var_dump($res);
        var_dump($val);

    }

main();
?>