我正在处理一个类,并且我有这个函数。我不知道错误在哪里,但函数根本没有"返回"任何东西,但如果我将"返回"更改为"echo",它确实显示正确的结果,并且逻辑似乎正在工作,所以我有点困惑……提前感谢。
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;
}
}
}
您在递归函数调用之前丢失了一个return
return $this->myFunction($value, $needle);
这是通过调用堆栈返回结果所必需的