dongxin5429 2016-07-14 19:29
浏览 57
已采纳

PHP - 函数返回和多维数组的问题

I have the next issue, I have this multidimensional array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [start_date] => 2016-07-14 07:00
                    [end_date] => 2016-07-14 07:20
                    [type] => 2
                )

            [1] => Array
                (
                    [start_date] => 2016-07-14 07:20
                    [end_date] => 2016-07-14 07:40
                    [type] => 2
                )

            [2] => Array
                (
                    [start_date] => 2016-07-14 07:40
                    [end_date] => 2016-07-14 08:00
                    [type] => 2
                )

            [3] => Array
                (
                    [start_date] => 2016-07-14 08:00
                    [end_date] => 2016-07-14 08:15
                    [type] => 3
                )

            [4] => Array
                (
                    [start_date] => 2016-07-14 08:15
                    [end_date] => 2016-07-14 08:30
                    [type] => 3
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [start_date] => 2016-07-15 07:00
                    [end_date] => 2016-07-15 07:20
                    [type] => 2
                )

            [1] => Array
                (
                    [start_date] => 2016-07-15 07:20
                    [end_date] => 2016-07-15 07:40
                    [type] => 2
                )

            [2] => Array
                (
                    [start_date] => 2016-07-15 07:40
                    [end_date] => 2016-07-15 08:00
                    [type] => 2
                )

            [3] => Array
                (
                    [start_date] => 2016-07-15 08:00
                    [end_date] => 2016-07-15 08:15
                    [type] => 3
                )

            [4] => Array
                (
                    [start_date] => 2016-07-15 08:15
                    [end_date] => 2016-07-15 08:30
                    [type] => 3
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [start_date] => 2016-07-18 07:00
                    [end_date] => 2016-07-18 07:20
                    [type] => 2
                )

            [1] => Array
                (
                    [start_date] => 2016-07-18 07:20
                    [end_date] => 2016-07-18 07:40
                    [type] => 2
                )

            [2] => Array
                (
                    [start_date] => 2016-07-18 07:40
                    [end_date] => 2016-07-18 08:00
                    [type] => 2
                )

            [3] => Array
                (
                    [start_date] => 2016-07-18 08:00
                    [end_date] => 2016-07-18 08:15
                    [type] => 3
                )

            [4] => Array
                (
                    [start_date] => 2016-07-18 08:15
                    [end_date] => 2016-07-18 08:30
                    [type] => 3
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [start_date] => 2016-07-19 07:00
                    [end_date] => 2016-07-19 07:20
                    [type] => 2
                )

            [1] => Array
                (
                    [start_date] => 2016-07-19 07:20
                    [end_date] => 2016-07-19 07:40
                    [type] => 2
                )

            [2] => Array
                (
                    [start_date] => 2016-07-19 07:40
                    [end_date] => 2016-07-19 08:00
                    [type] => 2
                )

            [3] => Array
                (
                    [start_date] => 2016-07-19 08:00
                    [end_date] => 2016-07-19 08:15
                    [type] => 3
                )

            [4] => Array
                (
                    [start_date] => 2016-07-19 08:15
                    [end_date] => 2016-07-19 08:30
                    [type] => 3
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [start_date] => 2016-07-20 07:00
                    [end_date] => 2016-07-20 07:20
                    [type] => 2
                )

            [1] => Array
                (
                    [start_date] => 2016-07-20 07:20
                    [end_date] => 2016-07-20 07:40
                    [type] => 2
                )

            [2] => Array
                (
                    [start_date] => 2016-07-20 07:40
                    [end_date] => 2016-07-20 08:00
                    [type] => 2
                )

            [3] => Array
                (
                    [start_date] => 2016-07-20 08:00
                    [end_date] => 2016-07-20 08:15
                    [type] => 3
                )

            [4] => Array
                (
                    [start_date] => 2016-07-20 08:15
                    [end_date] => 2016-07-20 08:30
                    [type] => 3
                )

        )

    [5] => Array
        (
            [0] => Array
                (
                    [start_date] => 2016-07-21 07:00
                    [end_date] => 2016-07-21 07:20
                    [type] => 2
                )

            [1] => Array
                (
                    [start_date] => 2016-07-21 07:20
                    [end_date] => 2016-07-21 07:40
                    [type] => 2
                )

            [2] => Array
                (
                    [start_date] => 2016-07-21 07:40
                    [end_date] => 2016-07-21 08:00
                    [type] => 2
                )

            [3] => Array
                (
                    [start_date] => 2016-07-21 08:00
                    [end_date] => 2016-07-21 08:15
                    [type] => 3
                )

            [4] => Array
                (
                    [start_date] => 2016-07-21 08:15
                    [end_date] => 2016-07-21 08:30
                    [type] => 3
                )

        )

    [6] => Array
        (
            [0] => Array
                (
                    [start_date] => 2016-07-22 07:00
                    [end_date] => 2016-07-22 07:20
                    [type] => 2
                )

            [1] => Array
                (
                    [start_date] => 2016-07-22 07:20
                    [end_date] => 2016-07-22 07:40
                    [type] => 2
                )

            [2] => Array
                (
                    [start_date] => 2016-07-22 07:40
                    [end_date] => 2016-07-22 08:00
                    [type] => 2
                )

            [3] => Array
                (
                    [start_date] => 2016-07-22 08:00
                    [end_date] => 2016-07-22 08:15
                    [type] => 3
                )

            [4] => Array
                (
                    [start_date] => 2016-07-22 08:15
                    [end_date] => 2016-07-22 08:30
                    [type] => 3
                )

        )

)

In addition, I have the following function:

public function transformValue($array = [], $level = 1) {
    $return = [];
    foreach($array as $key => $value){
        if(is_array($value)){
            $this->transformValue($value, $level + 1);
        } else{
            $return[$key] = $value;
        }
    }
    return $return;

}

This function receives this multi-array as a parameter, as I need to normalize it as follows:

Array
(
    [start_date] => 2016-07-14 07:00
    [end_date] => 2016-07-14 07:20
    [type] => 2
)
Array
(
    [start_date] => 2016-07-14 07:20
    [end_date] => 2016-07-14 07:40
    [type] => 2
)
Array
(
    [start_date] => 2016-07-14 07:40
    [end_date] => 2016-07-14 08:00
    [type] => 2
)
Array
(
    [start_date] => 2016-07-14 08:00
    [end_date] => 2016-07-14 08:15
    [type] => 3
)
Array
(
    [start_date] => 2016-07-14 08:15
    [end_date] => 2016-07-14 08:30
    [type] => 3
)
Array
(
    [start_date] => 2016-07-15 07:00
    [end_date] => 2016-07-15 07:20
    [type] => 2
)
Array
(
    [start_date] => 2016-07-15 07:20
    [end_date] => 2016-07-15 07:40
    [type] => 2
)
Array
(
    [start_date] => 2016-07-15 07:40
    [end_date] => 2016-07-15 08:00
    [type] => 2
)
Array
(
    [start_date] => 2016-07-15 08:00
    [end_date] => 2016-07-15 08:15
    [type] => 3
)
Array
(
    [start_date] => 2016-07-15 08:15
    [end_date] => 2016-07-15 08:30
    [type] => 3
)
Array
(
    [start_date] => 2016-07-18 07:00
    [end_date] => 2016-07-18 07:20
    [type] => 2
)
Array
(
    [start_date] => 2016-07-18 07:20
    [end_date] => 2016-07-18 07:40
    [type] => 2
)
Array
(
    [start_date] => 2016-07-18 07:40
    [end_date] => 2016-07-18 08:00
    [type] => 2
)
Array
(
    [start_date] => 2016-07-18 08:00
    [end_date] => 2016-07-18 08:15
    [type] => 3
)
Array
(
    [start_date] => 2016-07-18 08:15
    [end_date] => 2016-07-18 08:30
    [type] => 3
)
Array
(
    [start_date] => 2016-07-19 07:00
    [end_date] => 2016-07-19 07:20
    [type] => 2
)
Array
(
    [start_date] => 2016-07-19 07:20
    [end_date] => 2016-07-19 07:40
    [type] => 2
)
Array
(
    [start_date] => 2016-07-19 07:40
    [end_date] => 2016-07-19 08:00
    [type] => 2
)
Array
(
    [start_date] => 2016-07-19 08:00
    [end_date] => 2016-07-19 08:15
    [type] => 3
)
Array
(
    [start_date] => 2016-07-19 08:15
    [end_date] => 2016-07-19 08:30
    [type] => 3
)
Array
(
    [start_date] => 2016-07-20 07:00
    [end_date] => 2016-07-20 07:20
    [type] => 2
)
Array
(
    [start_date] => 2016-07-20 07:20
    [end_date] => 2016-07-20 07:40
    [type] => 2
)
Array
(
    [start_date] => 2016-07-20 07:40
    [end_date] => 2016-07-20 08:00
    [type] => 2
)
Array
(
    [start_date] => 2016-07-20 08:00
    [end_date] => 2016-07-20 08:15
    [type] => 3
)
Array
(
    [start_date] => 2016-07-20 08:15
    [end_date] => 2016-07-20 08:30
    [type] => 3
)
Array
(
    [start_date] => 2016-07-21 07:00
    [end_date] => 2016-07-21 07:20
    [type] => 2
)
Array
(
    [start_date] => 2016-07-21 07:20
    [end_date] => 2016-07-21 07:40
    [type] => 2
)
Array
(
    [start_date] => 2016-07-21 07:40
    [end_date] => 2016-07-21 08:00
    [type] => 2
)
Array
(
    [start_date] => 2016-07-21 08:00
    [end_date] => 2016-07-21 08:15
    [type] => 3
)
Array
(
    [start_date] => 2016-07-21 08:15
    [end_date] => 2016-07-21 08:30
    [type] => 3
)
Array
(
    [start_date] => 2016-07-22 07:00
    [end_date] => 2016-07-22 07:20
    [type] => 2
)
Array
(
    [start_date] => 2016-07-22 07:20
    [end_date] => 2016-07-22 07:40
    [type] => 2
)
Array
(
    [start_date] => 2016-07-22 07:40
    [end_date] => 2016-07-22 08:00
    [type] => 2
)
Array
(
    [start_date] => 2016-07-22 08:00
    [end_date] => 2016-07-22 08:15
    [type] => 3
)
Array
(
    [start_date] => 2016-07-22 08:15
    [end_date] => 2016-07-22 08:30
    [type] => 3
)

MY PROBLEM: When I call my function and assigned to a variable, it appears that the function does not assign any value to the variable, but if I check the return of the function with a print_r() shows the values as we need. I should be skipping is things and I need a "push" to continue. It is in this way that declare the variable with the return of function:

$return_value_function = [];
$return_value_function = $this->transformValue($array_values);

The variable $return_value_function does not save the function return. Where is my mistake?

  • 写回答

2条回答 默认 最新

  • douyan7916 2016-07-14 19:47
    关注

    You should assign the value from this call:

    $this->transformValue($value, $level + 1);

    to a variable. Like:

    $return[$key] = $this->transformValue($value, $level + 1);

    Everytime this function is called recursively, the return statement sends the data back & there is nothing to hold that returned data.

    SOLUTION

    You can change your function as below & achieve the same output without recursion:

    function transformValue($array = []) {
    $return = [];
    foreach($array as $key => $value) {
        if(is_array($value)) {
            $return = array_merge( $return, $value );
        }
    }
    return $return;
    }
    

    The output is:

    Array
    (
    [0] => Array
        (
            [start_date] => 01 Jul, 2016
            [type] => 1
        )
    
    [1] => Array
        (
            [start_date] => 02 Jul, 2016
            [type] => 1
        )
    
    [2] => Array
        (
            [start_date] => 03 Jul, 2016
            [type] => 1
        )
    
    [3] => Array
        (
            [start_date] => 04 Jul, 2016
            [type] => 1
        )
    
    [4] => Array
        (
            [start_date] => 05 Jul, 2016
            [type] => 1
        )
    
    [5] => Array
        (
            [start_date] => 06 Jul, 2016
            [type] => 2
        )
    
    [6] => Array
        (
            [start_date] => 07 Jul, 2016
            [type] => 2
        )
    
    [7] => Array
        (
            [start_date] => 08 Jul, 2016
            [type] => 2
        )
    
    [8] => Array
        (
            [start_date] => 09 Jul, 2016
            [type] => 2
        )
    
    [9] => Array
        (
            [start_date] => 10 Jul, 2016
            [type] => 2
        )
    
    [10] => Array
        (
            [start_date] => 11 Jul, 2016
            [type] => 3
        )
    
    [11] => Array
        (
            [start_date] => 12 Jul, 2016
            [type] => 3
        )
    
    [12] => Array
        (
            [start_date] => 13 Jul, 2016
            [type] => 3
        )
    
    [13] => Array
        (
            [start_date] => 14 Jul, 2016
            [type] => 3
        )
    
    [14] => Array
        (
            [start_date] => 15 Jul, 2016
            [type] => 3
        )
    
    [15] => Array
        (
            [start_date] => 16 Jul, 2016
            [type] => 4
        )
    
    [16] => Array
        (
            [start_date] => 17 Jul, 2016
            [type] => 4
        )
    
    [17] => Array
        (
            [start_date] => 18 Jul, 2016
            [type] => 4
        )
    
    [18] => Array
        (
            [start_date] => 19 Jul, 2016
            [type] => 4
        )
    
    [19] => Array
        (
            [start_date] => 20 Jul, 2016
            [type] => 4
        )
    
    [20] => Array
        (
            [start_date] => 21 Jul, 2016
            [type] => 5
        )
    
    [21] => Array
        (
            [start_date] => 22 Jul, 2016
            [type] => 5
        )
    
    [22] => Array
        (
            [start_date] => 23 Jul, 2016
            [type] => 5
        )
    
    [23] => Array
        (
            [start_date] => 24 Jul, 2016
            [type] => 5
        )
    
    [24] => Array
        (
            [start_date] => 25 Jul, 2016
            [type] => 5
        )
    )
    

    Please note my sample array only contains those 2 keys.

    And this output is returned through the following code:

    $return_value_function = [];
    $return_value_function = transformValue($arr);
    echo '<pre>';print_r($return_value_function);echo '</pre>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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