What is PHP array_merge() Function?
If you want to merge multiple arrays together to make one array, use array_merge() function. In the resultant array, it adds one array to the end of previous array. The function can combine values of both indexed and associative array or even mix of both.
Even you can merge one array or an empty array. You’ll learn detail about this in the coming sections.
Syntax:
array_merge(array1, array2, … arrayN)
Parameters:
The Function has 1 required parameter and any number of optional parameter-
array1 (Required): It is an array. It could be an indexed or associative array or an empty array.
array2, …arrayN (Optional): It could be one array or any number of arrays. Each array could be an indexed or associative array or an empty array.
Return Values:
The function returns the merged array. The function applies the following rules while merging arrays-
- The merged array contains the elements of the first array, followed by the elements of the next array, and so on. Remember this while applying the other rules. Check the example below-
<pre> <?php $arr1 = ["HTML", "CSS", "Javascript"]; $arr2 = ["PHP", "MySQL", "Ajax"]; $mergArr = array_merge($arr1, $arr2); print_r($mergArr); ?> </pre>
Output:
Array ( [0] => HTML [1] => CSS [2] => Javascript [3] => PHP [4] => MySQL [5] => Ajax )
Explanation:
The function merges all the elements of two arrays one after another.
- If the input arrays have same string keys, the function overrides the previous value by the later one. Check the example below-
<pre> <?php $arr1 = ["One" => "HTML", "Two" =>"CSS", "Three" =>"JavaScript"]; $arr2 = ["One" =>"PHP", "Two" =>"MySQL", "Four" =>"Ajax"]; $mergArr = array_merge($arr1, $arr2); print_r($mergArr); ?> </pre>
Output:
Array ( [One] => PHP [Two] => MySQL [Three] => JavaScript [Four] => Ajax )
Explanation:
As two arrays have same string keys “One”, the function replace the later value (“PHP”) by the previous value (“HTML”)
- If the input arrays have same numeric keys, the function doesn’t override the previous value by the later one or vice versa rather appends. And the numeric keys of the resultant array are renumbered starting from 0 and increases by 1. Check the example below-
<pre> <?php $arr1 = [0 => "HTML", 1 => "CSS", 10 => "JavaScript"]; $arr2 = [0 => "PHP", 1 => "MySQL", 6 => "Ajax"]; $mergArr = array_merge($arr1, $arr2); print_r($mergArr); ?> </pre>
Output:
Array ( [0] => HTML [1] => CSS [2] => JavaScript [3] => PHP [4] => MySQL [5] => Ajax )
Explanation:
When the function finds numeric keys in any array, it renumbers from 0 and increments by 1 for the subsequent numeric key values.
- If the arrays are mix of both numeric and string keys, the function follows the numeric key rules discussed above for the numeric key based values and applies string key rules for string keys based values. Check the example below-
<pre> <?php $arr1 = ["Language" => "HTML", 5, 1993, "Element" => "Yes", "Many"]; $arr2 = ["Language" => "PHP", 8.3, 1994, "Element" => "No", 1200 ]; $mergArr = array_merge($arr1, $arr2); print_r($mergArr); ?> </pre>
Output:
Array ( [Language] => PHP [0] => 5 [1] => 1993 [Element] => No [2] => Many [3] => 8.3 [4] => 1994 [5] => 1200 )
Explanation:
In case of the same string keys ex. “Language”. “Element”, the function replaces the later value by the previous one. So, PHP replaces HTML and No replaces Yes. As the remaining elements of the two arrays have numeric keys, the function renumbers the keys from zero and increments each keys by one.
- If you use only one numeric key array, in the merged array the integer keys start at 0 and increase by one. In case of string key array only, no change occurs. Check the example below-
<pre> <?php $arr1 = [0 => "HTML", 1=> "CSS", 10=> "Javascript"]; $mergArr = array_merge($arr1); print_r($mergArr); echo "<br />"; $arr = ["one"=>"PHP", "two"=>"MySQL", "four"=>"Python", "three"=>"JavaScript" ]; $mergArr = array_merge($arr); print_r($mergArr); ?> </pre>
Output:
Array ( [0] => HTML [1] => CSS [2] => Javascript ) Array ( [one] => PHP [two] => MySQL [four] => Python [three] => JavaScript )
Explanation:
When array_merge() takes one numeric key array only, it just renumbers the keys from 0 and increases each value by one. On the other hand, in case of an array with just string keys only, it does nothing.
- If you don’t use any array or in other word calling the function without any parameter, the function returns an empty array. Check the example below-
<pre> <?php $mergArr = array_merge(); print_r($mergArr); ?> </pre>
Output:
Array ( )
- Instead of an array, if you want to use a non-array type value, apply type casting on it and the function converts it to an array element. The function numbers it with an incrementing numeric key, or 0 if it the first parameter. Check the example below-
<pre> <?php $arr1 = (array) "String"; $arr2 = [0 => "PHP", 1=> "MySQL"]; $arr3 = (array) TRUE; $arr4 = (array) 100; $mergArr = array_merge($arr1, $arr2, $arr3, $arr4); print_r($mergArr); ?> </pre>
Output:
Array ( [0] => String [1] => PHP [2] => MySQL [3] => 1 [4] => 100 )
Explanation:
As the “String”, True, and 100 are string, Boolean, and integer type values and these are non-array type values, we type cast on these with (array), then we use those in the function. The function set numeric keys for those starting with 0 and increments by 1.
- If you try to merge multidimensional arrays, it only applies all the merging rules discussed above in level one array, and it can’t affect the level two or nested arrays. Check the example below. You can merge the nested array values by loop though.
<pre> <?php $arr1 = array("Language" => array("PHP", "Pythom"), array(5, 21), 100); $arr2 = array("Language" => array("C++", "Java"), array(1, 2), 7); $mergArr = array_merge($arr1, $arr2); print_r($mergArr); ?> </pre>
Output:
Array ( [Language] => Array ( [0] => C++ [1] => Java ) [0] => Array ( [0] => 5 [1] => 21 ) [1] => 100 [2] => Array ( [0] => 1 [1] => 2 ) [3] => 7 )
Another method of merging arrays (The UNION operator +):
If you want to merge multiple arrays one after another but don’t want to overwrite the elements of the next array by those of the previous one, use UNION operator (+). UNION operator preserves the numeric key numbers as those are; so, it doesn’t renumber the keys.
In case of same string key, array_merge() function overrides the previous value by the later one and UNION operator overrides the later value by the previous one.
In case of same numeric key, array_merge() function overrides the previous value by the later one and UNION operator overrides the later value by the previous one. Check the example below-
<pre>
<?php
$arr1 = [0 => "HTML", 1=> "CSS", "Type" => "Client", 10=> "Javascript"];
$arr2 = [0 => "PHP", 1=> "MySQL", 6=> "Python", 100, "Type" => "Server"];
$mergArr = $arr1 + $arr2;
print_r($mergArr);
?>
</pre>
Output:
Array ( [0] => HTML [1] => CSS [Type] => Client [10] => Javascript [6] => Python [7] => 100 )
Caution:
Even you quote numeric keys, the function considers it numeric keys, not string keys. Check the example below-
<pre>
<?php
$arr1 = ["0" => "HTML", "1" => "CSS", "10" => "Javascript"];
$arr2 = ["0" => "PHP", "1" => "MySQL", "6" => "Ajax"];
$mergArr = array_merge($arr1, $arr2);
print_r($mergArr);
?>
</pre>
Output:
Array ( [0] => HTML [1] => CSS [2] => Javascript [3] => PHP [4] => MySQL [5] => Ajax )
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP array_merge() Function
array_merge() is a useful built-in array function to merge any number of arrays into one. As now you know the rules and outcomes of both array_merge() and UNION operator (+), choose them based on your requirements.