Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
913 views
in Technique[技术] by (71.8m points)

recursion - Php doesn't break in a recursive foreach loop

I have a recursive function like below.

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

For Example

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

It gaves me

"founded"

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: finded

Array Structure is like

    [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
                                                    )

                                            )

                                    )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get the right value from recursion, your recursion call must not discard the return value. And since you want to walk back up the recursion tree as soon as you get a hit, and actually return the matching node, you have to break your loop at that point too.

Otherwise subsequent recursion calls overwrite your variable and the wrong node, false, or null is returned.

This should be what works:

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

Note: I removed the is_array because at that point $node has to be an array or throw an error at the first branch condition.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...