Home >>PHP Array Functions >PHP array_merge() Function
PHP array_merge() Function is used to merge(combines) one or more arrays into a single array.
Note : If the input arrays have the same string keys then the previous one will be overwritten by the later value for that key.
Note :If the arrays contain numerical keys, however, the later value will not replace the original value, but will be added.
Syntax:
array_merge(array1, array2, array3, ...)
Parameter | Description |
---|---|
array1 | This is required parameter. This parameter Specifies an array. |
array2 | This is optional parameter. This parameter Specifies an array. |
array3,... | This is optional parameter. This parameter Specifies an array. |
Here, is an example of array_merge() Function in PHP
<html> <body> <?php $a1=array("yellow","white"); $a2=array("green","voilet"); print_r(array_merge($a1,$a2)); ?> </body> </html>
Array ( [0] => yellow [1] => white [2] => green [3] => voilet )
Example 2:
<html> <body> <?php $a1=array("black","pink"); $a2=array("blue","red"); print_r(array_merge($a2,$a1)); ?> </body> </html>
Array ( [0] => blue [1] => red [2] => black [3] => pink )
Example 3(Merge 2 Associative array)
<html> <body> <?php $arr1=array("a"=>"red","b"=>"green"); $arr2=array("c"=>"blue","b"=>"yellow"); print_r(array_merge($arr1,$arr2)); ?> </body> </html>