dpbz14739 2017-09-08 20:48
浏览 42
已采纳

在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..

  • 写回答

1条回答 默认 最新

  • dpda53918 2017-09-08 22:18
    关注

    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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效