PHP - Error returning an array from a recursive function

After two hours of dizziness and googling - I'm stuck!

According to the header, I am trying to return the array that is created when the function passes. I only want to return the array variable to else, but it will not interact. It just returns as empty from the function, however inside else I can print_r and show as expected. It just won't return the array to the $ open_array variable. Any ideas (or abuses) would be greatly appreciated!

function find_parent($number, $open = false) {
   if(isset($other_variable[$number])) {
        foreach($other_variable[$number] as $val) {
           $open[$val->id] = [$val->id;
           $open = find_parent([$val->id, $open);

        }
   }
   else {
    return $open;
   }
}

$open_array = find_parent($number);
print_r($open_array);
+5
source share
1 answer

"then" $open, . , , - - , , else, .

, : , $open -. $open . . , .

$open, , .. , ( , , , ).

, , .

function find_parent($number, $open = false) { 
   if(isset($other_variable[$number])) { 
        foreach($other_variable[$number] as $val) { 
           $open[$val->id] = [$val->id; 
           $open = find_parent([$val->id, $open); 
           return $open; // INSERTED
        } 
   } 
   else { 
    return $open; 
   } 
} 
+3
source

All Articles