数组的php递归函数
我有一个多维数组(数组的php递归函数,php,arrays,recursion,Php,Arrays,Recursion,我有一个多维数组(MyArray),希望将其“线性化”为字符串。所以我尝试使用这个递归函数: function RecursiveFunction($TheArray){ foreach($TheArray as $key => $value){ if(is_array($value)){ $RecursiveOutput.="(".$key.")"; RecursiveFunction($value); //--&g
MyArray
),希望将其“线性化”为字符串。所以我尝试使用这个递归函数:
function RecursiveFunction($TheArray){
foreach($TheArray as $key => $value){
if(is_array($value)){
$RecursiveOutput.="(".$key.")";
RecursiveFunction($value); //-->this does't seem to work
} else {
$RecursiveOutput.="(".$value.")";
}
}
return $RecursiveOutput;
}
echo RecursiveFunction($MyArray);
然而,我只从数组的第一级获取键:递归调用似乎不起作用。有人能发现问题吗?您正在返回
$RecursiveOutput
,但没有捕获返回值。试试这个
$RecursiveOutput .= "(". $key .")(". RecursiveFunction($value) .")";
您没有选择需要执行的内部函数调用的返回值。您正在返回
$RecursiveOutput
,但没有捕获返回值。请尝试此$RecursiveOutput.=“($key.)”(.RecursiveFunction($value.”)”)”代码>或者您可以通过引用传递变量?@RisulIslam就是这样<代码>$RecursiveOutput.=“($key.)”(“.RecursiveFunction($value)。”)”代码>。张贴作为接受的答案。谢谢!