Array_merge_recursive没有正确合并(php)

时间:2015-05-28 07:29:24

标签: php arrays

以下两个数组并未在[2015]中合并,就像他们应该的那样。相反,它创建[2016],就像在上一个例子中一样。

代码我使用:

$ merge = array_merge_recursive($ array,$ current);

第一个数组:( $ current)

Array
(
    [Particulier] => Array
        (
            [Weekend] => Array
                (
                    [2015] => Array
                        (
                            [06] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 111
                                            [startDate] => 12-06-2015
                                            [endDate] => 15-06-2015
                                            [price] => 920
                                        )
                                )
                        )
                )
        )
)

第二个数组:($ array)

Array
(
    [Particulier] => Array
        (
            [Weekend] => Array
                (
                    [2015] => Array
                        (
                            [05] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 90
                                            [startDate] => 01-05-2015
                                            [endDate] => 04-05-2015
                                            [price] => 920
                                        )
                                    [1] => Array
                                        (
                                            [id] => 91
                                            [startDate] => 08-05-2015
                                            [endDate] => 11-05-2015
                                            [price] => 920
                                        )
                                    [2] => Array
                                        (
                                            [id] => 92
                                            [startDate] => 15-05-2015
                                            [endDate] => 18-05-2015
                                            [price] => 920
                                        )
                                )
                            [06] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 112
                                            [startDate] => 19-06-2015
                                            [endDate] => 22-06-2015
                                            [price] => 920
                                        )
                                )
                        )
                )
            [Midweek] => Array
                (
                    [2015] => Array
                        (
                            [05] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 94
                                            [startDate] => 11-05-2015
                                            [endDate] => 15-05-2015
                                            [price] => 1225
                                        )
                                    [1] => Array
                                        (
                                            [id] => 95
                                            [startDate] => 18-05-2015
                                            [endDate] => 22-05-2015
                                            [price] => 1225
                                        )
                                )
                            [06] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 118
                                            [startDate] => 15-06-2015
                                            [endDate] => 19-06-2015
                                            [price] => 1225
                                        )
                                )
                        )
                )
        )
    [Clienten en patienten] => Array
        (
            [Weekend] => Array
                (
                    [2015] => Array
                        (
                            [05] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 105
                                            [startDate] => 01-05-2015
                                            [endDate] => 04-05-2015
                                            [price] => 645
                                        )
                                )
                        )
                )
        )
)

当前结果:($ merge)

Array
(
    [Particulier] => Array
        (
            [Weekend] => Array
                (
                    [2015] => Array
                        (
                            [05] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 90
                                            [startDate] => 01-05-2015
                                            [endDate] => 04-05-2015
                                            [price] => 920
                                        )
                                [1] => Array
                                    (
                                        [id] => 91
                                        [startDate] => 08-05-2015
                                        [endDate] => 11-05-2015
                                        [price] => 920
                                    )

                                [2] => Array
                                    (
                                        [id] => 92
                                        [startDate] => 15-05-2015
                                        [endDate] => 18-05-2015
                                        [price] => 920
                                    )
                            )
                        [06] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 112
                                        [startDate] => 19-06-2015
                                        [endDate] => 22-06-2015
                                        [price] => 920
                                    )
                            )
                    )
                [2016] => Array
                    (
                        [06] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 111
                                        [startDate] => 12-06-2015
                                        [endDate] => 15-06-2015
                                        [price] => 920
                                    )
                            )
                    )
            )

编辑:预期结果

Array
(
    [Particulier] => Array
    (
        [Weekend] => Array
        (
            [2015] => Array
            (
                [05] => Array
                (
                    [0] => Array
                    (
                        [id] => 90
                        [startDate] => 01-05-2015
                        [endDate] => 04-05-2015
                        [price] => 920
                    )

                    [1] => Array
                    (
                        [id] => 91
                        [startDate] => 08-05-2015
                        [endDate] => 11-05-2015
                        [price] => 920
                    )

                    [2] => Array
                    (
                        [id] => 92
                        [startDate] => 15-05-2015
                        [endDate] => 18-05-2015
                        [price] => 920
                    )

                )

                [06] => Array
                (
                    [0] => Array
                    (
                        [id] => 112
                        [startDate] => 19-06-2015
                        [endDate] => 22-06-2015
                        [price] => 920
                    )
                    [1] => Array
                    (
                        [id] => 111
                        [startDate] => 12-06-2015
                        [endDate] => 15-06-2015
                        [price] => 920
                    )
                )
            )
        )

如果需要任何其他信息,请询问! 提前谢谢!

1 个答案:

答案 0 :(得分:2)

根据您的描述,您必须使用关联数组(使用字符串索引,如" 2015")而不是数字索引(如2015)。

我还建议使用唯一键作为事件的索引,并使用替代函数array_replace_recursive来阻止PHP创建任何嵌套数组(参见http://php.net/manual/en/function.array-replace-recursive.php