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.
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.
$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(+).
Result of array_merge() funcion Array ( [zero] => 0 [one] => 11 [two] => 2  => 10  => 11  => 12  => 13 [three] => 3 [four] => 4  => 12  => 13  => 14  => 15 ) Result of array union(+) operator Array ( [zero] => 0 [one] => 1 [two] => 2  => 10  => 11  => 12  => 13 [three] => 3 [four] => 4 )
- Difference and Similarities between PHP and C
- Difference between array() and  in PHP
- Difference between try-catch and if-else statements in PHP
- Difference between isset() and array_key_exists() Function in PHP
- What is the difference between the | and || or operator in php?
- What is the difference between public, private, and protected in PHP?
- Difference between require-dev and require in PHP?
- What is the difference between HTTP_HOST and SERVER_NAME in PHP?
- What is the difference between is_a() function and instanceof in PHP?
- What is the difference between echo, print, and print_r in PHP?
- What is the difference between a language construct and a “built-in” function in PHP ?
- Difference between “!==” and “==!” in PHP
- What is the difference between single-quoted and double-quoted strings in PHP?
- Difference between break and continue in PHP
- What is the difference between Perl and PHP ?
- Difference Between JSP and PHP
- What is the difference between fopen modes “r+”, "rw+" and “w+” in PHP?
- Difference Between “php artisan dump-autoload” and “composer dump-autoload”
- Difference between file_get_contents and cURL in PHP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.