Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数组的php递归函数_Php_Arrays_Recursion - Fatal编程技术网

数组的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)。”)”。张贴作为接受的答案。谢谢!