Php没有';递归foreach循环中的t中断

Php没有';递归foreach循环中的t中断,php,recursion,foreach,return,break,Php,Recursion,Foreach,Return,Break,我有一个递归函数,如下所示 public function findnodeintree($cats,$cat_id) { foreach($cats as $node) { if((int)$node['id'] == $cat_id) { echo "finded"; $finded = $node; bre

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

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
                                                    )

                                            )

                                    )

将递归行更改为:

 $finded = $this->findnodeintree($node['children'],$cat_id);
您希望能够获取该行,如果调用函数,则该行必须填充变量,否则它将在最后发生的情况下填充,但不会将结果返回到第一次调用

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

递归调用findNodeTree将经过某些循环,不会“查找”任何内容,但它们仍然返回变量$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;
}

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

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

这应该是可行的:

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
必须是一个数组,或者在第一个分支条件下抛出一个错误