0

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?

2
  • Each array has 2 arrays in it, so merge each of those arrays with their counterparts, not the main array. Commented Jul 28, 2017 at 15:33
  • Have you tried array_merge_recursive()? Commented Jul 28, 2017 at 15:36

4 Answers 4

1

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
        )

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

2 Comments

Genius. Is this adaptable if I have more than 2 arrays? if I wanted to merge 3 arrays together into one?
Sure, I added a variant for that as an UPDATE to the answer.
0

You want to create a multi dimensional array?

That's pretty easy.

$new_array[] = $array_1;
$new_array[] = $array_2;

2 Comments

Try your own code. You will see that it does not produce the output as described in the question.
You are right, I got ahead of myself, and didn't finish the question.
0

you can try this approach:

    $array1 = [
        [1,2,3,4],
        [6,7,8,9]
    ];
    $array2 = [
        ['a','b'],
        ['c', 'd']
    ];
    $array3 = [];
    foreach (range(0, max(count($array1), count($array2))-1) as $i) {
        $part1 = isset($array1[$i]) ? $array1[$i] : [];
        $part2 = isset($array2[$i]) ? $array2[$i] : [];
        $array3[] = array_merge($part1, $part2);
    }

Comments

0

What about this with array_merge_recursive?

array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

$result = array_merge_recursive($first_array, $second_array);

EDIT: If array_merge_recursive doesn't work for you then try this https://eval.in/838950

<?php
$first_array = [
    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
    ]
];
$second_array = [
    1 => [
        'TIME',
        'Lorem',
        'Ipsum'
    ],
    2 => [
        'TIME',
        'Some',
        'Text'
    ]
];

foreach($second_array as $k=>$v){
 $first_array[$k] = array_merge($frist_array[$k],$v);
}


 print '<pre>';
 /*print_r(array_merge_recursive($first_array,$second_array));*/      
 print_r($frist_array);

1 Comment

I tried this but It did not work, See edited question for the output I am getting with array_merge() and array_merge_recursive()

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.