October 13, 2024
Merging two or more arrays in PHP using array_merge function
There are times when we need to merge two and more array, for the same in PHP we have a build in function called array_merge. The array_merge() function in PHP takes two or more arrays as arguments and joins them into one big array. If two arrays have the same key, the value from the second array replaces the one from the first. If the keys are just numbers, it adds the values to the end and reorder the array.
Code
$array1 = ["apple1", "apple2"];
$array2 = ["apple3", "apple4"];
$result = array_merge($array1, $array2);
print_r($result);
Output
Array
(
[0] => apple1
[1] => apple2
[2] => apple3
[3] => apple4
)