The return value of a recursive method in PHP

Source: Internet
Author: User
{Code ...} this method is used to obtain the corresponding val value for the Mark $ key. I return $ v at the end of the method, and then I assign the return result to a value $ va, this value is always blank, but I have a value when printing $ v at the end of the method. why? Is variable...

  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; // here there is a value of 1 // echo $ v; // Test to directly output} $ va = search_val ('6a34', $ array); var_dump ($ va); // $ va is always empty

This method is to mark$keyRetrieve the correspondingvalAt the end of the method.return $vAnd then IreturnThe result is assigned to a value.$vaThis value is always null, but I have a value when printing $ v at the end of the method. why? Is it caused by the scope of variables or by nonstandard code writing? Solution

Reply content:

  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; // here there is a value of 1 // echo $ v; // Test to directly output} $ va = search_val ('6a34', $ array); var_dump ($ va); // $ va is always empty

This method is to mark$keyRetrieve the correspondingvalAt the end of the method.return $vAnd then IreturnThe result is assigned to a value.$vaThis value is always null, but I have a value when printing $ v at the end of the method. why? Is it caused by the scope of variables or by nonstandard code writing? Solution

The function is changed to the following. your logic is a bit problematic. return is used for data interaction between methods. The final value obtained can be search_val, which is returned for you. Therefore, return is directly returned here.

 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);        }        

To put it bluntly, recursion means calling itself.
In your code, function calls do not obtain their return values.
Pay attention to the processing of the returned results.

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.