2

The following 2 array's are not merging right at [2015] like they're supposed to. instead it creates [2016] like in the last example.

code i'm using :

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

first array: ($current)

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

second array: ($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
                                        )
                                )
                        )
                )
        )
)

current result: ($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
                                    )
                            )
                    )
            )

EDIT: expected result

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

If any other information is required, please ask! Thanks in advance!

1
  • 1
    expected result please? Commented May 28, 2015 at 7:32

1 Answer 1

3

From what you describe you will have to work with an associated array (using a string index like "2015") and not a numeric index (like 2015).

I would also suggest using a unique key as the index for the events and to use the alternative function array_replace_recursive to prevent PHP from creating any nested arrays (see http://php.net/manual/en/function.array-replace-recursive.php)

Sign up to request clarification or add additional context in comments.

2 Comments

I'm creating the array through a foreach loop. How do I get a string index then? And doesn't array_replace_recursive just overwrite the previous record? That's not what I want. I want it to add.
Please post the foreach loop code generating the array if that is the problem. Array replace only overwrites existing entries. Using a unique key you will never overwrite any of your entries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.