在php中循环从一个函数跳转到另一个函数的一些问题[关闭]

在php中循环从一个函数跳转到另一个函数的一些问题[关闭]

问题描述:

i noticed a wired behavior of php when doing something like

the code below is trying to loop through all arrays in multiarray, and it it should return $n when it is not an array anymore.
basicly a counter for dimentions.

<?php 
//some random values multiarray
$arrays[1][2][3][5] = '1';
$arrays[1][2][3] = '1';
$arrays[1][2][3][4] = '1';
$arrays[3][6][3][1][5] = '2';

function second_func($array, $n)//adding $n then returning to 1st func
{
    $n++;
    first_func($array, $n);
}
function first_func($arrays, $n = 0)//checking if array then 2nd func, if not then return.
{
    if(is_array($arrays))
    { 
        foreach($arrays as $array)second_func($array, $n); 
    }else
    {
        echo $n; //working
        return $n; //not working
    };
}
$result[] = first_func($arrays);
print_r($result);//nothing here

or any similar actions, if i put file_put_contents inside 1st function, then the result will be different with each refresh, and the function cannot be returned just as it is, only echoing it works stable.

I remember i had a similar 2 function to delete cache files and ended up with clean hard drive, or a similar func to create file and i would have files in random folders over hd.

didnt check for similar quastions, cannot formulate the quastion right..

我注意到在执行类似 p>

代码时的php的有线行为 下面是试图循环遍历多个数组中的所有数组,当它不再是数组时它应该返回$ n。
基本上是一个维数的计数器。 p>

   &lt;?php 
 //一些随机值multiarray 
 $ arrays [1] [2] [3] [5] ='1'; 
 $ arrays [1] [2] [3] ='1'  ; 
 $ arrays [1] [2] [3] [4] ='1'; 
 $ arrays [3] [6] [3] [1] [5] ='2'; 
 
  second_func($ array,$ n)//添加$ n然后返回1st func 
 {
 $ n ++; 
 first_func($ array,$ n); 
} 
函数first_func($ arrays,$ n =  0)//检查数组是否然后是第二个函数,如果没有那么返回。
 {
 if if(is_array($ arrays))
 {
 foreach($ arrays as $ array)second_func($ array,$ n)  ;  
} else 
 {
 echo $ n;  // working 
 return $ n;  //不工作
}; 
} 
 $ result [] = first_func($ arrays); 
print_r($ result); //这里没什么
  code>  pre> 
 
 或任何类似的动作,如果我将file_put_contents放在第一个函数中,那么每个刷新的结果都会不同,并且该函数不能像原来那样返回,只能回显它的工作稳定。 p> 
 
  

我记得我有一个类似的2功能删除缓存文件,最后是干净的硬盘驱动器,或类似的func来创建文件,我会在高清随机文件夹中有文件。 p> 没有检查类似的规则,无法制定正确的判决.. p> div>

The problem with your code is that the first_func function does not always return a value, only when the argument is not an array. But since you call it initially with an array argument, that particular call will not return anything, and this explains the blank output you get. So, even when you call a function recursively (via second_func), you must still return something.

If I understand correctly, you want an output per "leaf" in the tree of nested arrays: for each leaf, its depth in the tree should be output.

Here is how you could do that:

function first_func($arrays, $n = 0) {
    if(is_array($arrays)) {
        $result = [];
        foreach($arrays as $array)
            $result = array_merge($result, first_func($array, $n+1));
    } else {
        $result = [$n];
    }
    return $result;
}

This will return an array of integers, where each integer represents the depth of a particular leaf in the data.

Note that I removed second_func, as it is quite trivial. Writing first_func($arrays, $n+1) is not less clear than second_func($array, $n). If however you want to keep second_func, then make sure to return a value:

function second_func($array, $n) {
    $n++;
    return first_func($array, $n);
}

For this data:

$arrays = [
    1 => [
        2 => [
            3 => "1   1"
        ]
    ],
    3 => [
        6 => [
            3 => [
                1 => [
                    5 => "2"
                ]
            ]
        ]
    ]
];

... the above code will return:

[3, 5]

Which can be formatted as 35 with:

echo implode("", first_func($arrays));