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) {foreach ($ arras $ v) {ask the experts to modify the recursive function so that it can 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; }