Ask the experts to help modify a recursive function to make it return values

Source: Internet
Author: User
Please help me modify the recursive function so that it has a returned value. Currently, I have a recursive function. its function is to echo the ID value in a multi-dimensional array and display it as "1, 2, 3, 4" on the page, 5... "PHPcodefunctionarr_fun ($ arr) {if (is_array ($ arr) {ask the experts to modify the recursive function and let it return values.
At present, I have a recursive function, which is used to echo the ID value in a multi-dimensional array.
Echo is displayed as "1, 2, 3, 4, 5..." on the page ..."
PHP code
  function arr_fun($arr){        if(is_array($arr)){          foreach($arr as $v){             if(is_array($v)){             arr_fun($v);             }else{                  echo $arr[id].",";                break;           }         }        }else{             echo $arr.",";        }  } 


Because I need to use this string of IDs from echo
Failed to assign a value to $ s = arr_fun ($ array_menu); note: $ array_menu is a dynamic multi-dimensional array with dynamic dimensions.
So I think the above method does not return values, so I cannot assign values.
I hope my predecessors will give me some advice and help me modify it. how can I make the returned value "1, 2, 3, 4 ..."?


------ Solution --------------------
PHP code
function arr_fun($arr){        $r = '';      if(is_array($arr)){          foreach($arr as $v){             if(is_array($v)){             $r .= arr_fun($v);             }else{                  $r .= $arr[id].",";                break;           }         }        }else{             $r .= $arr.",";        }      return $r;  }

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.