-1

I have two arrays (in the foreach loop) that may or may not have the same dynamically generated keys e.g. 298, 184, 182 as in the code sample below.

I want to merge values for the same keys and make a single array.

Note: Arrays are the results within the foreach loop.
Keys are dynamically generated by id and items may or may not be the same in both arrays.

Resulting Arrays in Loop

Array
(
    [298] => Array
        (
            [dates] => Array
                (
                    [0] => 2020-12-26 10:39:13
                    [1] => 2020-12-15 12:18:23
                    [2] => 2020-12-22 12:27:46
                    [3] => 2020-12-24 10:51:00
                    [4] => 2020-12-26 10:01:44
                    [5] => 2020-12-26 10:31:55
                    [6] => 2020-12-26 10:48:33
                    [7] => 2020-12-26 10:56:34
                )

        )

    [184] => Array
        (
            [dates] => Array
                (
                    [0] => 2020-11-16 07:52:33
                    [1] => 2020-12-24 13:01:14
                )

        )

    [182] => Array
        (
            [dates] => Array
                (
                    [0] => 2021-01-05 08:50:27
                )

        )

)
Array
(
    [298] => Array
        (
            [dates] => Array
                (
                    [0] => 2020-11-16 07:38:26
                    [1] => 2020-11-16 07:47:05
                    [2] => 2020-12-15 12:20:07
                    [3] => 2020-12-24 10:52:35
                )

        )

    [184] => Array
        (
            [dates] => Array
                (
                    [0] => 2020-11-16 07:53:12
                )

        )

)

Expecting Single Array

Array
(
    [298] => Array
        (
            [dates] => Array
                (
                    [0] => 2020-12-26 10:39:13
                    [1] => 2020-12-15 12:18:23
                    [2] => 2020-12-22 12:27:46
                    [3] => 2020-12-24 10:51:00
                    [4] => 2020-12-26 10:01:44
                    [5] => 2020-12-26 10:31:55
                    [6] => 2020-12-26 10:48:33
                    [7] => 2020-12-26 10:56:34
                    [8] => 2020-11-16 07:38:26
                    [9] => 2020-11-16 07:47:05
                    [10] => 2020-12-15 12:20:07
                    [11] => 2020-12-24 10:52:35
                )

        )

    [184] => Array
        (
            [dates] => Array
                (
                    [0] => 2020-11-16 07:52:33
                    [1] => 2020-12-24 13:01:14
                    [2] => 2020-11-16 07:53:12
                )

        )

    [182] => Array
        (
            [dates] => Array
                (
                    [0] => 2021-01-05 08:50:27
                )

        )

)
Code Lover
  • 8,099
  • 20
  • 84
  • 154

1 Answers1

1

Assuming that the second array can also have keys that are not in the first, you can find all the unique keys from both arrays, and then iterate over them, merging the values from array1 and array2 into a result array:

$keys = array_unique(array_merge(array_keys($array1), array_keys($array2)));

$result = array();
foreach ($keys as $key) {
    $result[$key]['dates'] = array_merge($array1[$key]['dates'] ?? [], $array2[$key]['dates'] ?? []);
}
print_r($result);

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • Thanks a lot. You are a genious. I was trying this for two days with various loops but no luck. I appreciate your help. – Code Lover Jan 05 '21 at 08:37