Merge two arrays

I have two arrays:

array( '11' => '11', '22' => '22', '33' => '33', '44' => '44' ); array( '44' => '44', '55' => '55', '66' => '66', '77' => '77' ); 

I want to combine these two arrays so that they do not contain duplicates and do not retain their original keys. For example, the output should be:

 array( '11' => '11', '22' => '22', '33' => '33', '44' => '44', '55' => '55', '66' => '66', '77' => '77' ); 

I tried this, but it changes its source keys:

 $output = array_unique( array_merge( $array1 , $array2 ) ); 

Any solution?

+75
arrays php unique
Jun 30 2018-11-11T00:
source share
8 answers

Just use:

 $output = array_merge($array1, $array2); 

That should solve it. Since you use string keys, if one key occurs more than once (for example, '44' in your example), one key will overwrite outgoing with the same name. Because in your case, they both have the same value in any case, it does not matter, and it will also remove duplicates.

Update: I just realized that PHP treats numeric string keys as numbers (integers) and therefore will behave in a way that means it also renumbers the keys ...

A workaround is to recreate the keys.

 $output = array_combine($output, $output); 

Update 2: I always forget that there is an operator (in bold because this is really what you are looking for !: D)

 $output = $array1 + $array2; 

All of this can be seen at: http://php.net/manual/en/function.array-merge.php

+142
Jun 30 '11 at 13:25
source share

You should take into account that $array1 + $array2 != $array2 + $array1

 $array1 = array( '11' => 'x1', '22' => 'x1' ); $array2 = array( '22' => 'x2', '33' => 'x2' ); 

with $ array1 + $ array2

 $array1 + $array2 = array( '11' => 'x1', '22' => 'x1', '33' => 'x2' ); 

and with the array $ array2 + $ array1

 $array2 + $array1 = array( '11' => 'x1', '22' => 'x2', '33' => 'x2' ); 
+30
Oct 26 '12 at 13:23
source share

It works:

 $output = $array1 + $array2; 
+24
Jun 30 2018-11-11T00:
source share

To do this, you can iterate over one and add to the other:

 <?php $test1 = array( '11' => '11', '22' => '22', '33' => '33', '44' => '44' ); $test2 = array( '44' => '44', '55' => '55', '66' => '66', '77' => '77' ); function combineWithKeys($array1, $array2) { foreach($array1 as $key=>$value) $array2[$key] = $value; asort($array2); return $array2; } print_r(combineWithKeys($test1, $test2)); ?> 

UPDATE: KingCrunch has developed a better solution : print_r($array1+$array2);

+4
Jun 30 2018-11-11T00:
source share

It works:

 $a = array(1 => 1, 2 => 2, 3 => 3); $b = array(4 => 4, 5 => 5, 6 => 6); $c = $a + $b; print_r($c); 
+1
Jun 30 2018-11-11T00:
source share

Attention! $ array1 + $ array2 overwrites the keys, so my solution (for multi-dimensional arrays) is to use array_unique ()

 array_unique(array_merge($a, $b), SORT_REGULAR); 

Note:

5.2.10+ Changed the default value of sort_flags to SORT_STRING.

5.2.9 The default value is SORT_REGULAR.

5.2.8. The default value is SORT_STRING

works great . Hope this helps.

+1
Nov 21 '17 at 12:10
source share

If you are using PHP 7.4 or higher, you can use the distribution operator ... as the following examples from PHP Docs:

 $arr1 = [1, 2, 3]; $arr2 = [...$arr1]; //[1, 2, 3] $arr3 = [0, ...$arr1]; //[0, 1, 2, 3] $arr4 = array(...$arr1, ...$arr2, 111); //[1, 2, 3, 1, 2, 3, 111] $arr5 = [...$arr1, ...$arr1]; //[1, 2, 3, 1, 2, 3] function getArr() { return ['a', 'b']; } $arr6 = [...getArr(), 'c']; //['a', 'b', 'c'] $arr7 = [...new ArrayIterator(['a', 'b', 'c'])]; //['a', 'b', 'c'] function arrGen() { for($i = 11; $i < 15; $i++) { yield $i; } } $arr8 = [...arrGen()]; //[11, 12, 13, 14] 

This works as in JavaScript ES6.

Read more at https://wiki.php.net/rfc/spread_operator_for_array .

+1
Jul 26 '19 at 17:25
source share

https://www.php.net/manual/en/function.array-merge.php

 <?php $array1 = array("color" => "red", 2, 4); $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge($array1, $array2); print_r($result); ?> 
0
Aug 05 '19 at 12:31 on
source share



All Articles