0

I'm working on a class, and I have this function. I don't know where the mistake is but function doesn't "return" anything at all, yet if I change the "return" for an "echo" it does display the correct result, and the logic seems to be working, so I'm a little confused..Thanks in advance.

    public function myFunction(array $haystack, $needle)
{   

    foreach($haystack as $key => $value )
    {            
        if (is_array($value))
        {  
             $this->myFunction($value, $needle);
        }
        else
        {  
            if($key == $needle) 
            { 
                return $value; 
                break;
            }
        }   
    }
sbayaz
  • 3
  • 1

1 Answers1

1

You're missing a return before the recursive function call.

return $this->myFunction($value, $needle);

This is necessary to return the result through the call stack.

Dave Morrissey
  • 4,371
  • 1
  • 23
  • 31