【问题标题】:PHP - Problems with function return and multidimensional arrayPHP - 函数返回和多维数组的问题
【发布时间】:2016-07-14 19:29:21
【问题描述】:

我有下一个问题,我有这个多维数组:

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 不保存函数返回。我的错在哪里?

【问题讨论】:

  • $this->transformValue($value, $level + 1); 返回什么以及返回什么变量?
  • 我不明白$level 的用途。
  • @Chay22 $level 用于确定数组的深度,函数是递归的。
  • @u_mulder $level 在函数中初始化为1并增加其值直到找到多数组中的值
  • 你明白调用$this->transformValue($value, $level + 1) 会将一些数据返回到一个void 中吗?

标签: php function multidimensional-array


【解决方案1】:

你应该从这个调用中赋值:

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

到一个变量。喜欢:

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

每次递归调用此函数时,return 语句会将数据发回,并且没有任何内容可以保存返回的数据。

解决方案

您可以如下更改您的函数并在不递归的情况下实现相同的输出:

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

请注意,我的示例数组仅包含这 2 个键。

并且这个输出是通过下面的代码返回的:

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

【讨论】:

  • 谢谢,但问题不在于功能。如果我在函数返回之前调用 print_r($return),会正确显示值,但变量 $return_value_function 不采用函数返回的值
  • 太棒了。你能把这个标记为正确答案吗?
【解决方案2】:

请参考以下代码。它会根据需要返回数组。

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

【讨论】:

    猜你喜欢
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2017-03-06
    • 2013-03-08
    相关资源
    最近更新 更多