Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge values of the second level of two arrays with different number of keys?

Tags:

arrays

php

I have following arrays:

$array1 = "
    Array (
        [0] => Array (
                   [0] => Value1
                   [1] => Value2
               )

        [1] => Array (
                   [0] => Value3
                   [1] => Value4
               )

        [2] => Array (
                   [0] => Value5
                   [1] => Value6
               )
        ...
        [999] => Array (
                   [0] => Value7
                   [1] => Value8
               )
    )

and

$array2 = "
    Array (
        [0] => Array (
                   [0] => ValA
                   [1] => ValB
                   [2] => ValC
                   [3] => ValD
               )

        [1] => Array (
                   [0] => ValE
                   [1] => ValF
                   [2] => ValG
                   [3] => ValH
               )

        [2] => Array (
                   [0] => ValI
                   [1] => ValJ
                   [2] => ValK
                   [3] => ValL
               )
        ...
        [999] => Array (
                   [0] => ValM
                   [1] => ValN
                   [2] => ValO
                   [3] => ValP
               )
    )

I would like to merge both arrays so that in the end I have the same structure, number of keys in the first level, but the values would be "joined" in the second level just like we can see in the following result array:

$array3 = "
    Array (
        [0] => Array (
                   [0] => Value1
                   [1] => Value2
                   [2] => ValA
                   [3] => ValB
                   [4] => ValC
                   [5] => ValD
               )

        [1] => Array (
                   [0] => Value3
                   [1] => Value4
                   [2] => ValE
                   [3] => ValF
                   [4] => ValG
                   [5] => ValH
               )

        [2] => Array (
                   [0] => Value5
                   [1] => Value6
                   [2] => ValI
                   [3] => ValJ
                   [4] => ValK
                   [5] => ValL
               )
        ...
        [999] => Array (
                   [0] => Value7
                   [1] => Value8
                   [2] => ValM
                   [3] => ValN
                   [4] => ValO
                   [5] => ValP
               )
    )

I tried with array_merge and array_merge_recursive as showed in this solution: https://stackoverflow.com/a/16541831/3499881, but it didn't work.

  • Do I really have to convert all numeric keys to string or is it somehow also possible with that keys? If this is the only way, could you please show me how I can convert it?

  • Or is the problem caused because in the first array there are 2 keys and in the second 4?

I would appreciate any clue. Many thanks in advance.

like image 200
Aloysia de Argenteuil Avatar asked Jul 03 '14 14:07

Aloysia de Argenteuil


People also ask

How do you merge two arrays?

To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.

How do you merge two arrays using the spread operator?

To combine two or more arrays, you can either use the functional method []. concat(arr1, arr2) or the spread operator [...arr1,...arr2]. The merging outcome is kept in a new array, making these methods immutable.

How can I merge two arrays in PHP without duplicates?

You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.


2 Answers

There is an "elegant" way to resolve your issue (it will require, however, that both of your arrays must have same count of items):

$result = array_map('array_merge', $array1, $array2);

Check the fiddle.

Answering to your question: array_merge_recursive() will fail because it appends numeric keys, not merging them:

If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

like image 50
Alma Do Avatar answered Oct 31 '22 07:10

Alma Do


You could loop over $array1, and merge where the key matches in $array2:

$array3 = array();
foreach ($array1 as $key => $a1) {
  if (array_key_exists($key, $array2)) {
    $array3[$key] = array_merge($a1, $array2[$key]);
  } else {
    $array3[$key] = $a1;
  }
}

The only limitation with the above is that if keys exist in $array2, but not in $array1, they won't be added to the array (that's not a problem in your case). Another solution would be to collect all of the keys from both arrays, and then loop over and perform the merge:

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

foreach ($keys as $key) {
  if (array_key_exists($key, $array1) && array_key_exists($key, $array2)) {
    $array3[$key] = array_merge($array1[$key], $array2[$key]);
  } elseif (array_key_exists($key, $array1)) {
    $array3[$key] = $array1[$key];
  } else {
    $array3[$key] = $array2[$key];
  }
}
like image 3
billyonecan Avatar answered Oct 31 '22 06:10

billyonecan