PHP Array Merge: How to Combine Arrays in PHP
In PHP, there are several ways to merge two arrays:
- Utilize the array_merge() function:
 
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$mergedArray = array_merge($array1, $array2);
- Use the “+” operator:
 
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$mergedArray = $array1 + $array2;
- Utilize the array_merge_recursive() function for merging multidimensional arrays.
 
$array1 = ['a' => 'apple', 'b' => 'banana'];
$array2 = ['a' => 'apricot', 'c' => 'cherry'];
$mergedArray = array_merge_recursive($array1, $array2);
It is important to note that the array_merge() and array_merge_recursive() functions will reindex the array, while the “+” operator will preserve the original array keys.