The php array_merge() function allows us to append the items of one or more arrays to the items of a given array.
$array1 = array("bear","elephant","lion");
$array2 = array(1,2,3);
$array3 = array("orange","red","yellow");
print_r(array_merge($array1,$array2,$array3));
// Output:
Array
(
[0] => bear
[1] => elephant
[2] => lion
[3] => 1
[4] => 2
[5] => 3
[6] => orange
[7] => red
[8] => yellow
)
When working with collections of data in php, the ability to add to, remove from, and manipulate those collections is valuable.
One such operation which is useful is the ability to be able to append items to an array.
We can append the items of multiple arrays together using the php array_merge() function.
The array_merge() functions “merges” one or more arrays together where the values of the arrays are appended to each other to form a new array.
Below is a simple example of how to use the array_merge() function in php.
$array1 = array("bear","elephant","lion");
$array2 = array(1,2,3);
$array3 = array("orange","red","yellow");
print_r(array_merge($array1,$array2,$array3));
// Output:
Array
(
[0] => bear
[1] => elephant
[2] => lion
[3] => 1
[4] => 2
[5] => 3
[6] => orange
[7] => red
[8] => yellow
)
Merging Arrays with Duplicate Keys with array_merge() in php
When working with array_merge(), there are a few things you need to be aware of when it comes to how the keys are determined after merging.
If the arrays you are merging with array_merge() are numeric, then the keys will be reindex with the keys starting from zero in the resulting array.
This was shown above in the first example. Below shows this example again, where all of the arrays have numeric keys.
$array1 = array("bear","elephant","lion");
$array2 = array("orange","red","yellow");
print_r($array1);
print_r($array2);
print_r(array_merge($array1,$array2));
// Output:
Array
(
[0] => bear
[1] => elephant
[2] => lion
)
Array
(
[0] => orange
[1] => red
[2] => yellow
)
Array
(
[0] => bear
[1] => elephant
[2] => lion
[3] => orange
[4] => red
[5] => yellow
)
Things are a little more complicated when there are string keys in the arrays you are merging. If you are merging arrays that have keys which are the same, then the last value will overwrite the previous values.
Below is an example showing how array_merge() works with arrays that have string keys.
$array1 = array("black" => "bear", "grey" => "elephant", "yellow" => "lion");
$array2 = array("orange" => "orange", "red" => "tomato", "yellow" => "lemon");
print_r($array1);
print_r($array2);
print_r(array_merge($array1,$array2));
// Output:
Array
(
[black] => bear
[grey] => elephant
[yellow] => lion
)
Array
(
[orange] => orange
[red] => tomato
[yellow] => lemon
)
Array
(
[black] => bear
[grey] => elephant
[yellow] => lemon
[orange] => orange
[red] => tomato
)
As you can see, since both arrays have the key ‘yellow’, only the last value, “lemon” in the second array was kept after merging.
Hopefully this article has been useful for you to learn how to use the php array_merge() function.