合并具有多个键的数组(php)

4
下面的数组是我的当前情况。通过循环,新数据被添加进来。 我尝试使用'array_merge_recursive'以及这个被接受的答案。但它似乎不起作用,或者我使用方法不正确。
Array (
    [0] => Array (
            [Customer] => Array (
                    [Weekend] => Array (
                            [2016] => Array (
                                    [01] => Array (
                                            [0] => Array (
                                                    [id] => 54
                                                    [startDate] => 01-01-2016
                                                    [endDate] => 31-12-2016
                                                    [price] => 0
                                                )
                                        )
                                )
                        )
                )
        )
    [1] => Array (
            [Customer] => Array (
                    [Weekend] => Array (
                            [2018] => Array (
                                    [01] => Array (
                                            [0] => Array (
                                                    [id] => 56
                                                    [startDate] => 01-01-2018
                                                    [endDate] => 31-12-2018
                                                    [price] => 0
                                                )
                                        )
                                )
                        )
                )
        )
    [2] => Array (
            [Customer] => Array (
                    [Weekend] => Array (
                            [2019] => Array (
                                    [01] => Array (
                                            [0] => Array (
                                                    [id] => 57
                                                    [startDate] => 01-01-2019
                                                    [endDate] => 31-12-2019
                                                    [price] => 0
                                                )
                                        )
                                )
                        )
                )
        )
)

希望的情况是这样的:

期望的情况是这样的:

Array (
                [Customer] => Array (
                    [Weekend] => Array (
                        [2016] => Array (
                            [01] => Array (
                                [0] => Array (
                                    [id] => 54
                                    [startDate] => 01-01-2016
                                    [endDate] => 31-12-2016
                                    [price] => 0
                                )
                            )
                        )
                        [2018] => Array (
                            [01] => Array (
                                [0] => Array (
                                    [id] => 56
                                    [startDate] => 01-01-2018
                                    [endDate] => 31-12-2018
                                    [price] => 0
                                )
                            )
                        )
                        [2019] => Array (
                            [01] => Array (
                                [0] => Array (
                                    [id] => 57
                                    [startDate] => 01-01-2019
                                    [endDate] => 31-12-2019
                                    [price] => 0
                                )
                            )
                        )
                    )
                )
            )

如果需要其他信息,请问一下!我是新来的。

如果您在此代码上执行array_values会发生什么? - Chris
@Chris:然后你会得到相同的数组。 - AbraCadaver
2个回答

4
我相信这会起作用:
$result = call_user_func_array('array_merge_recursive', $array);

比我的容易多了 呵呵。 - Styphon
在原始数组中,我使用了“客户”和“客户端”的ID作为键,为了使它更清晰,在SO上进行了更改。这就是为什么“array_merge_recursive”之前无法工作的原因。但是,使用名称作为键它可以工作!非常感谢! - user3481441

0
这应该可以满足你的需求:
$new_arr = [];
$arr = // Your current array;

foreach ($arr as $customer)
{
    foreach ($customer['Weekend'] as $year => $z)
    {
        foreach($z as $month => $y)
        {
            foreach($y as $entry)
            {
                Store($year, $month, $entry, $new_arr);
            }
        }
    }

}

function Store($year, $month, $entry, & $new_arr)
{
    if ( ! isset($new_arr['customer'][$year]))
    {
        $new_arr['customer'][$year] = array();
    }
    if ( ! isset($new_arr['customer'][$year][$month]))
    {
        $new_arr['customer'][$year][$month] = array();
    }
    $new_arr['customer'][$year][$month][] = $entry;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接