-3

I have two arrays, and I want to merge them in one array. I have added both of the arrays and also the desired output.

Array1:

Array
(
    [0] => Array
        (
            [0] => b561d2e627efd2
            [1] => d561d2e627f0cc
            [2] => f561d2e627f17a
            [3] => g561d2e627f1d1
        )

    [1] => Array
        (
            [0] => b561d2e627f632
            [1] => d561d2e627f71f
            [2] => f561d2e627f7d1
            [3] => g561d2e627f823
        )
)

Array2:

Array
(
    [0] => Array
        (
            [0] => c561d2e627f378
            [1] => e561d2e627f425
            [2] => b561d2e627efd2
        )

    [1] => Array
        (
            [0] => c561d2e627f9ee
            [1] => e561d2e627fa78
            [2] => b561d2e627f632
        )
)

Required Output:

Array
(
    [0] => Array
        (
            [0] => b561d2e627efd2
            [1] => d561d2e627f0cc
            [2] => f561d2e627f17a
            [3] => g561d2e627f1d1
            [4] => c561d2e627f378
            [5] => e561d2e627f425
            [6] => b561d2e627efd2
        )

    [1] => Array
        (
            [0] => b561d2e627f632
            [1] => d561d2e627f71f
            [2] => f561d2e627f7d1
            [3] => g561d2e627f823
            [4] => c561d2e627f9ee
            [5] => e561d2e627fa78
            [6] => b561d2e627f632
        )
)
halfer
  • 19,824
  • 17
  • 99
  • 186
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88

3 Answers3

2

Simple. Iterate over your first array, get the current key, use that key to get the second array's chunk, use array_merge to merge them, then push them to a new array. Do the following:

$arr1 = array(
    array("one", "two", "three"),
    array("ten", "eleven", "twelve")
);
$arr2 = array(
    array("four", "five", "six"),
    array("thirteen", "fourteen", "fifteen")
);

foreach ($arr1 as $k => $arr1_chunk) {
    $arr2_chunk = $arr2[$k];
    $final[] = array_merge($arr1_chunk, $arr2_chunk);
}
var_dump($final);

Result:

array (size=2)
  0 => 
    array (size=6)
      0 => string 'one' (length=3)
      1 => string 'two' (length=3)
      2 => string 'three' (length=5)
      3 => string 'four' (length=4)
      4 => string 'five' (length=4)
      5 => string 'six' (length=3)
  1 => 
    array (size=6)
      0 => string 'ten' (length=3)
      1 => string 'eleven' (length=6)
      2 => string 'twelve' (length=6)
      3 => string 'thirteen' (length=8)
      4 => string 'fourteen' (length=8)
      5 => string 'fifteen' (length=7)
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
0

You dont suggest any need to handle duplicate values, and both arrays seem to have the same number of 1st level elements, so simply pushing the contents of array 2 into array1 should do the job, with a simple nested loop:

foreach($array1 as $k=>$v){
    foreach($array2[$k] as $val){
        $array1[$k][] = $val;
    }
}
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
Steve
  • 20,703
  • 5
  • 41
  • 67
  • Why was it downvoted twice? – al'ein Oct 14 '15 at 13:00
  • @CodeGodie Presumably it was accepted because it did as the OP asked - `$array1` would match the expected output. I mention specifically that it is a naive method that does not handle duplicates or arrays of differing lengths, but the OP never suggested that was a requirement in the 1st place. – Steve Oct 14 '15 at 13:33
  • you know what. You are right. I thought your `$array1` variable inside your `foreach` was a different variable. I will remove my comment and upvote now. Thanks for the feedback. – CodeGodie Oct 14 '15 at 13:36
-2

You could use array_merge to "flatten" these arrays.

Here's how it would work in the example you referenced above assuming that the first array is called $array1 and the second array is called array2

$new_array = array_merge($array1[0], $array1[1]);
$new_array2 = array_merge($array2[0], $array2[1]);
print_r($new_array);
print_r($new_array2);
  • this will not work because the merge needs to happen with the second array, meaning `array1[0]` with `array2[0]` – CodeGodie Oct 14 '15 at 13:32