What is the difference between array_merge and array + array in PHP?

In PHP arrays can be joined using array union (+) operator or by using array_merge() function. There is a subtle differences in these two methods. The array_merge() function is an inbuilt function for joining two array regardless of their type (numerical, categorical etc.)

array_merge() Function: The array_merge() merges one or more arrays provided as input and provide a new array as output. In this merging process the values of an array is appended to the end of the previous array to generate the resulting array.

Syntax:



array array_merge( $arr1, $arr2, $arr3... )

Parameters: The array_merge() function accepts one or more input array and merges them into single resulting array.

Note: In the array_merge() function if the input arrays have same string keys (in case of categorical arrays) then in the resulting array the later value of the key will overwrite the previous one. But if the array contain numeric keys (in case of numeric arrays) then the values will not be replaced they will simply get appended in the resulting array. Also in case of numeric array the value of the keys will be renumbered starting from zero in the resulting array.

Array union (+) operator: Another way to merge two array is the array union(+) operator. It is a binary operator that means it merges two arrays at a time. The union operator appends the right hand array at the end of the left hand array.

Syntax:

$arr3 = $arr1 + $arr2

Parameters: The union(+) operator works on two arrays at a time and produce the resulting array.

Note: In case of array union(+) operator the keys which are same in both the arrays the values from the left hand array corresponding to the keys will be taken in the resulting array. Also in case of numeric array the index of the right hand array which is same with left hand array will be ignored in resultant array.

Program: PHP code to explain difference between array_merge() and array union(+).

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Merge two arrays where one key ('one') is same
      
$arr1 = array( 'zero' => 0,
               'one' => 1,
               'two' => 2, 10, 11, 12, 13
        );
          
$arr2 = array( 'one' => 11,
               'three' => 3,
               'four' => 4, 12, 13, 14, 15
        );
      
// Merging both array using array_merge() function
  
// Here in $arr3 the value corresponding to
// the key 'one' will be from $arr2 and 
// numeric keys will be renumbered
      
$arr3 = array_merge($arr1, $arr2);
      
echo "Result of array_merge() funcion\n";
  
print_r($arr3);
      
// Merging both array using array union(+) operator
// Here in $arr4 the value corresponding to the key
// 'one' will be from $arr1 and numeric keys
// which are repeated in $arr2 will be ignored
      
$arr4 = $arr1 + $arr2;
      
echo "\nResult of array union(+) operator\n";
  
print_r($arr4);
  
?>

chevron_right


Output:

Result of array_merge() funcion
Array
(
    [zero] => 0
    [one] => 11
    [two] => 2
    [0] => 10
    [1] => 11
    [2] => 12
    [3] => 13
    [three] => 3
    [four] => 4
    [4] => 12
    [5] => 13
    [6] => 14
    [7] => 15
)

Result of array union(+) operator
Array
(
    [zero] => 0
    [one] => 1
    [two] => 2
    [0] => 10
    [1] => 11
    [2] => 12
    [3] => 13
    [three] => 3
    [four] => 4
)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.