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 between array() and  in PHP
- Difference between String and Character array in Java
- Difference Between indexOf and findIndex function of array
- Array.BinarySearch(Array, Object) Method with examples in C#
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
- How to sort an Array in C# | Array.Sort() Method Set – 2
- How to re-index an array in PHP?
- Kotlin Array
- C# | Array vs ArrayList
- PHP append one array to another
- PHP | array() Function
- How to get the first element of an array in PHP?
- How to get random value out of an array 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 email@example.com. 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.