Php doesn't break in a recursive foreach loop

break, foreach, php, recursion, return

Solution

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.

Problem

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

Original source