Php 不会在递归 foreach 循环中中断


Php doesn't break in a recursive foreach loop

我有一个递归函数,如下所示。

public function findnodeintree($cats,$cat_id)
{       
    foreach($cats as $node)
    {                   
        if((int)$node['id'] == $cat_id)
        {       
            echo "finded";
            $finded = $node;
            break;
        }
        else
        {
            if(is_array($node) && array_key_exists('children', $node)){ 
                $this->findnodeintree($node['children'],$cat_id);
            }
        }           
    }
    return $finded;
}

例如

$node =$this->findnodeintree($category_Array, 169);

它给了我

"founded"

遇到 PHP 错误

Severity: Notice
Message: Undefined variable: finded

数组结构是这样的

    [0] => Array
    (
        [id] => 0
        [name] => MAIN CATEGORY
        [depth] => 0
        [lft] => 1
        [rgt] => 296
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 167
                        [name] =>  CAT 0
                        [depth] => 1
                        [lft] => 2
                        [rgt] => 17
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 169
                                        [name] =>   CAT 1
                                        [depth] => 2
                                        [lft] => 3
                                        [rgt] => 4
                                    )
                                [1] => Array
                                    (
                                        [id] => 170
                                        [name] =>   CAT 2
                                        [depth] => 2
                                        [lft] => 5
                                        [rgt] => 10
                                        [children] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [id] => 171
                                                        [name] =>    CAT 5
                                                        [depth] => 3
                                                        [lft] => 6
                                                        [rgt] => 7
                                                    )
                                                [1] => Array
                                                    (
                                                        [id] => 172
                                                        [name] =>    CAT 3
                                                        [depth] => 3
                                                        [lft] => 8
                                                        [rgt] => 9
                                                    )
                                            )
                                    )

若要从递归中获取正确的值,递归调用不得丢弃返回值。而且由于您希望在命中后立即返回递归树,并实际返回匹配节点,因此此时也必须中断循环。

否则,后续递归调用将覆盖变量并返回错误的节点、falsenull

这应该是有效的:

public function findnodeintree($cats,$cat_id)
{       
    foreach($cats as $node)
    {                   
        if((int)$node['id'] == $cat_id){       
            return $node;
        }
        elseif(array_key_exists('children', $node)) {
            $r = $this->findnodeintree($node['children'], $cat_id);
            if($r !== null){
                return $r;
            }
        }           
    }
    return null;
}

注意:我删除了is_array,因为此时$node必须是数组或在第一个分支条件中抛出错误。

将递归行更改为:

 $finded = $this->findnodeintree($node['children'],$cat_id);

您希望能够获取该行,如果您还记得该函数,则该行必须填充您的变量,否则它将在最后一次出现的情况下填充,但永远不会将结果返回到您的第一次调用。

因此,$finded将在您的第一次调用中为空或不存在。

归调用findnodeintree将经历某些循环不会"找到"任何东西,但它们仍然返回一个变量$finded。除了由于在他们的循环中从未找到它,所以确实没有声明该变量。试试这个:

public function findnodeintree($cats,$cat_id)
{       
    $finded = NULL;
    foreach($cats as $node)
    {                   
        if((int)$node['id'] == $cat_id)
        {       
            echo "finded";
            $finded = $node;
            break;
        }
        else
        {
            if(is_array($node) && array_key_exists('children', $node)){ 
                $this->findnodeintree($node['children'],$cat_id);
            }
        }           
    }
    return $finded;
}