array_merge() concatenates one or more arrays provided as input and provides a new array as output. This merge process adds array values to the end of the previous array to generate the resulting array.Syntax:
array array_merge ($arr1, $arr2, $arr3 ...) Parameters:
The array_merge() function takes one or more input arrays and concatenates them into one resulting array.Note.
In the array_merge() function, if the input arrays have the same string keys (in the case of categorical arrays), the later key value in the resulting array will overwrite the previous one. But if the array contains numeric keys (in the case of numeric arrays), then the values will not be replaced, they will simply be added to the resulting array. Also, in the case of a numeric array, the value of the keys will be renumbered starting from zero in the resulting array.The array concatenation operator (+).
Another way to combine two arrays is the array concatenation operator (+) ... It is a binary operator, which means that it concatenates two arrays at the same time. The union operator appends the right array to the end of the left array.Syntax:
$arr3 = $arr1 + $arr2 Parameters:
the union (+) operator works simultaneously with two arrays and creates the resulting array.Note.
In the case of the array union operator (+), keys that match in both arrays, in the resulting array the values from the left array corresponding to the keys will be taken. Also in the case of a numeric array, the index of the right array that is the same as the left array will be ignored in the resulting array.Program:
PHP code explaining the difference between array_merge() and array concatenation (+).
// Combine two arrays where one key (’ one ’) is the same
= > 0,
= > 1,
= > 2, 10, 11, 12, 13
= > 11,
= > 3,
= > 4, 12, 13, 14, 15
// Combine both arrays using the array_merge() function
// Here in $arr3 the value corresponding
// key ’ one ’ will be from $arr2 and
// numeric keys will be renumbered
" Result of array_merge() funcion "
// Combine both arrays using the union operator (+)
// Here in $arr4 the value corresponding to the key
// ’ one ’ will be $arr1 and number keys
// which are repeated in $arr2 will be ignored
" Result of array union (+) operator "
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 [ 3] = > 13 [three] = > 3 [four] = > 4)