Home > Article > Backend Development > Problem with returning value from a recursive method in PHP
<code><?php $array = array( 0 => array( '1a' => '', '3a' => '', '6a' => array( '6a1' => '', '6a2' => '', '6a3' => array( '6a31' => '', '6a33' => '', '6a34' => '1', ), ), ), 1 => array( '3b' => '', '4b' => array( '4b1' => '', '4b2' => '', ), '6b' => '', ) ); function search_val($key, $arr) { $v = ''; foreach ($arr as $ks => $vs) { if($ks != $key && is_array($vs)) { search_val($key, $vs); }else if($ks != $key && is_string($vs)) { continue; }else if($ks == $key) { $v = $vs; break; } } return $v; //var_dump($v);exit; //这里是有值的 为1 //echo $v; //测试这样可以直接输出 } $va = search_val('6a34', $array); var_dump($va); //$va始终为空 </code>
The meaning of this method is to get the value corresponding to val
for the subscript $key
. I return $v
at the end of the method, and then I assign the result of this return
to a value $va
, this The value is always empty, but when I print $v at the end of the method, it has a value. Why is this? Is it a problem with variable scope or is it caused by irregularities in my code? Solve
<code><?php $array = array( 0 => array( '1a' => '', '3a' => '', '6a' => array( '6a1' => '', '6a2' => '', '6a3' => array( '6a31' => '', '6a33' => '', '6a34' => '1', ), ), ), 1 => array( '3b' => '', '4b' => array( '4b1' => '', '4b2' => '', ), '6b' => '', ) ); function search_val($key, $arr) { $v = ''; foreach ($arr as $ks => $vs) { if($ks != $key && is_array($vs)) { search_val($key, $vs); }else if($ks != $key && is_string($vs)) { continue; }else if($ks == $key) { $v = $vs; break; } } return $v; //var_dump($v);exit; //这里是有值的 为1 //echo $v; //测试这样可以直接输出 } $va = search_val('6a34', $array); var_dump($va); //$va始终为空 </code>
The meaning of this method is to get the value corresponding to val
for the subscript $key
. I return $v
at the end of the method, and then I assign the result of this return
to a value $va
, this The value is always empty, but when I print $v at the end of the method, it has a value. Why is this? Is it a problem with variable scope or is it caused by irregularities in my code? Solve
Function is changed to the following. There is something wrong with your logic. You need to use return for data interaction between methods. The final value you get can be regarded as what search_val returns to you, so just return it here.
<code class="php"> function search_val($key, $arr) { foreach ($arr as $ks => $vs) { if($ks != $key && is_array($vs)) { return search_val($key, $vs); }else if($ks != $key && is_string($vs)) { continue; }else if($ks == $key) { return $vs; } } //return $v; //var_dump($v); } </code>
To put it bluntly, recursion means calling yourself.
The function call itself in your code does not get its return value.
Secondly, pay attention to the processing of the results returned by yourself.
<code>function search_val($key, $arr) { if (!is_array($arr) || empty($arr)) { return false; } else { foreach ($arr as $k => $v) { if ($k === $key) { return $v; } if (is_array($v) && !empty($v)) { $re = search_val($key, $v); if ($re !== false) { return $re; } } continue; } } return false; }</code>