PHP - 函数返回和多维数组

时间:2016-07-14 19:29:22

标签: php function 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
                )

        )

)

另外,我有以下功能:

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;

}

此函数接收此多数组作为参数,因为我需要按如下方式对其进行规范化:

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
)

我的问题:当我调用我的函数并分配给变量时,似乎该函数没有为变量赋值,但是如果我用print_r检查函数的返回()显示我们需要的值。我应该跳过的是事情,我需要一个“推动”继续。通过这种方式,使用函数返回声明变量:

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

变量$ return_value_function不保存函数返回。我的错误在哪里?

2 个答案:

答案 0 :(得分:1)

您应该从此调用中分配值:

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

变量。像:

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

每次递归调用此函数时,return语句都会将数据发送回&没有什么可以保存返回的数据。

<强>解

您可以更改以下功能&amp;在没有递归的情况下实现相同的输出:

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

输出结果为:

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
    )
)

请注意我的示例数组只包含这两个键。

此输出通过以下代码返回:

$return_value_function = [];
$return_value_function = transformValue($arr);
echo '<pre>';print_r($return_value_function);echo '</pre>';

答案 1 :(得分:0)

请参阅以下代码。它会根据您的意愿返回数组。

public function transformValue($array) {        
    foreach($array as $subarray){
        foreach($subarray as $value){
            $finalArray[] = $value;
        }
    }
    return $finalArray;

}

$finalArray= $this->transformValue($array_values);
echo "<pre>";
print_r($finalArray);