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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名