A problem with the return value of a recursive method in PHP

Source: Internet
Author: User
 !--? 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;  Here is the value for 1//echo $v; The test can be directly output} $va = Search_val (' 6a34 ', $array); Var_dump ($VA); $va is always empty  

The meaning of this method is to give $key the label out val the corresponding value, I at the end of the method return $v , and then I return assign the result to a value $va , this value is always empty, but I was the method at the end of the printing $v is a value, why this? Is it a variable scope problem or is my code written in a non-canonical cause? Solving

  • 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.