Heim >Backend-Entwicklung >PHP-Tutorial > 请高手帮忙修改一上递归函数,让其有返回值
请高手帮忙修改一下递归函数,让其有返回值
目前我有一递归函数,其作用是echo出一多维数组中的ID值
echo到页面上显示为“1,2,3,4,5...”
<!-- 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.","; } }
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>