请高手帮忙修改一上递归函数,让其有返回值

WBOY
Release: 2016-06-13 13:14:40
Original
799 people have browsed it

请高手帮忙修改一下递归函数,让其有返回值
目前我有一递归函数,其作用是echo出一多维数组中的ID值
echo到页面上显示为“1,2,3,4,5...”

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->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.",";  
      }
  } 
Copy after login


由于我需要利用echo出来的这串ID字符串
尝试赋值给$s不成功 $s=arr_fun($array_menu); 备注:$array_menu是一动态多维数组,维数是动态变化的
所以我想应该是上述方法没有返回值,所以没法儿赋值
希望前辈赐教,帮我修改一下,怎么才能让其返回值也是“1,2,3,4...”呢


------解决方案--------------------
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;
  } <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!