Search code examples
phparraysarray-merge

Merge an array with an existing array php


I have two array both containing data. I want to be able to add them together so that the information from the second array joins into the first array. Currently the array_merge that I am doing adds the second array to the end of the first one.

Array 1

[1] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 16.11
            [4] => 80.56
            [5] => 96.67
        )
[2] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 1.23
            [4] => 50.69
            [5] => 14.24
        )

Array 2

[1] => Array
        (
            [0] => TIME
            [1] => Lorem
            [2] => Ipsum
        )

[2] => Array
        (
            [0] => TIME
            [1] => Some
            [2] => Text

        )

How can I Merge the two arrays so the output becomes like below?

Array 3

[1] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 16.11
            [4] => 80.56
            [5] => 96.67
            [6] => TIME
            [7] => Lorem
            [8] => Ipsum
        )
[2] => Array
        (
            [0] => 2017-07-13 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 1.23
            [4] => 50.69
            [5] => 14.24
            [6] => TIME
            [7] => Some
            [8] => Text
        )

What is currently happening

[1] => Array
        (
            [0] => 2017-07-14 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 16.11
            [4] => 80.56
            [5] => 96.67
        )

    [2] => Array
        (
            [0] => 2017-07-14 00:00:00
            [1] => Foo
            [2] => Bar
            [3] => 1.23
            [4] => 50.69
            [5] => 14.24
        )

    [3] => Array
        (
            [0] => TIME
            [1] => Lorem
            [2] => Ipsum
        )

    [4] => Array
        (
            [0] => TIME
            [1] => Some
            [2] => Text
        )

I have tried array_merge( $array1 , $array2 ); but that adds the second array to the end of the first one.

Any ideas?


Solution

  • This looks pretty forward to me, you simply array_merge() the elements of those two arrays:

    <?php
    $A = [
        1 => [
            '2017-07-13 00:00:00',
            'Foo',
            'Bar',
            16.11,
            80.56,
            96.67
        ],
        2 => [
            '2017-07-13 00:00:00',
            'Foo',
            'Bar',
            1.23,
            50.69,
            14.24
        ]
    ];
    $B = [
        1 => [
            'TIME',
            'Lorem',
            'Ipsum'
        ],
        2 => [
            'TIME',
            'Some',
            'Text'
        ]
    ];
    
    array_walk($B, function($values, $key) use (&$A) {
        $A[$key] = array_merge($A[$key], $values);
    });
    print_r($A);
    

    The output of that obviously is:

    Array
    (
        [1] => Array
            (
                [0] => 2017-07-13 00:00:00
                [1] => Foo
                [2] => Bar
                [3] => 16.11
                [4] => 80.56
                [5] => 96.67
                [6] => TIME
                [7] => Lorem
                [8] => Ipsum
            )
    
        [2] => Array
            (
                [0] => 2017-07-13 00:00:00
                [1] => Foo
                [2] => Bar
                [3] => 1.23
                [4] => 50.69
                [5] => 14.24
                [6] => TIME
                [7] => Some
                [8] => Text
            )
    )
    

    UPDATE:

    In the comment below you ask if this approach can be generalized to merge an arbitrary number of arrays. Sure that is possible, you just add another iteration layer:

    <?php
    $target = [
        1 => ['2017-07-13 00:00:00', 'Foo', 'Bar', 16.11, 80.56, 96.67],
        2 => ['2017-07-13 00:00:00', 'Foo', 'Bar', 1.23, 50.69, 14.24]
    ];
    $sources = [
        'B' => [
            1 => ['TIME', 'Lorem', 'Ipsum'],
            2 => ['TIME', 'Some', 'Text']
        ],
        'C' => [
            1 => ['C1a', 'C1b'],
            2 => ['C2a', 'C2b', 'C2b']
        ]
    ];
    
    array_walk($sources, function($source) use (&$target) {
        array_walk($source, function($values, $key) use (&$target) {
            $target[$key] = array_merge($target[$key], $values);
        });
    });
    print_r($target);
    

    This variant produces that output:

    Array
    (
        [1] => Array
            (
                [0] => 2017-07-13 00:00:00
                [1] => Foo
                [2] => Bar
                [3] => 16.11
                [4] => 80.56
                [5] => 96.67
                [6] => TIME
                [7] => Lorem
                [8] => Ipsum
                [9] => C1a
                [10] => C1b
            )
    
        [2] => Array
            (
                [0] => 2017-07-13 00:00:00
                [1] => Foo
                [2] => Bar
                [3] => 1.23
                [4] => 50.69
                [5] => 14.24
                [6] => TIME
                [7] => Some
                [8] => Text
                [9] => C2a
                [10] => C2b
                [11] => C2b
            )
    
    )