PHP Array Merge: An In-Depth Guide
The PHP array_merge() function is a powerful tool for combining arrays into a single array. This function takes two or more arrays as arguments and merges them into a single array, with the values of the later arrays overwriting the values of the previous arrays when keys match. Note: In PHP 8, passing non-array arguments to array_merge() will throw a TypeError. In this guide, we will dive deep into the array_merge() function, exploring its various uses and applications.
Basic Usage of array_merge()
The basic usage of the array_merge() function is straightforward. Simply pass in two or more arrays as arguments, and the function will return a new array that is the result of merging the arrays. Here is an example:
PHP array_merge function example
<?php
$array1 = ["color" => "red", 2, 4];
$array2 = ["a", "b", "color" => "green", "shape" => "rectangle", 4];
$result = array_merge($array1, $array2);
print_r($result);
?>Output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => rectangle
[4] => 4
)As you can see, the values from $array2 overwrite the values of $array1 when they have the same key.
Merging Multidimensional Arrays
The array_merge() function can also be used to merge multidimensional arrays. To merge multidimensional arrays, simply pass in each array as an argument to the function. Here is an example:
PHP To merge multidimensional arrays by using array_merge_recursive
<?php
$array1 = ["color" => ["favorite" => "red"], 2, 4];
$array2 = ["a", "b", "color" => ["favorite" => "green"], "shape" => "rectangle", 4];
$result = array_merge($array1, $array2);
print_r($result);
?>Output:
Array
(
[color] => Array
(
[favorite] => green
)
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => rectangle
[4] => 4
)Note that array_merge() can merge multidimensional arrays, but it will overwrite nested arrays that share the same key. To append nested arrays instead of overwriting them, use array_merge_recursive().
Merging an Indexed Array with an Associative Array
When merging an indexed array with an associative array, the values from both arrays are combined. Here is an example:
PHP Example of merging an indexed array with an associative array by array_merge function
<?php
$array1 = ["a", "b", "c"];
$array2 = ["a" => "apple", "b" => "banana", "c" => "cherry"];
$result = array_merge($array1, $array2);
print_r($result);
?>Output:
Array
(
[0] => a
[1] => b
[2] => c
[a] => apple
[b] => banana
[c] => cherry
)As you can see, the values from both arrays are combined. Since the keys are different types (numeric vs. string), no overwriting occurs. This is an important detail to keep in mind when merging arrays with different key types.
Merging an Associative Array with an Indexed Array
The result of merging an associative array with an indexed array is similar. The values are combined, and the keys are preserved according to their type. Here is an example:
PHP merging an associative array with an indexed array by using array_merge function
<?php
$array1 = ["a" => "apple", "b" => "banana", "c" => "cherry"];
$array2 = ["a", "b", "c"];
$result = array_merge($array1, $array2);
print_r($result);
?>Output:
Array
(
[a] => apple
[b] => banana
[c] => cherry
[0] => a
[1] => b
[2] => c
)As shown, the values combine without conflict. String keys from $array1 are preserved, while numeric keys from $array2 are reindexed starting from 0.
Performance Considerations
The array_merge() function has a linear time complexity, meaning that the time it takes to merge arrays increases linearly with the size of the arrays. For this reason, it's important to keep the number of arrays being merged to a minimum, and to avoid using the function on extremely large arrays.
Conclusion
The array_merge() function is a versatile tool for combining arrays in PHP. Whether you're merging flat arrays, multidimensional arrays, indexed arrays, or associative arrays, this function has you covered. With its linear time complexity, it's important to keep the size of the arrays being merged to a minimum, but with the right approach, this function can be an invaluable tool in your PHP toolkit.
Practice
What is true about the array_merge function in PHP?