Combine two arrays into one array:
<?php $a1 = array ( " a " => " red " , " b " => " green " ) ; $a2 = array ( " c " => " blue " , " b " => " yellow " ) ; print_r ( array_merge ( $a1 , $a2 ) ) ; ?>The array_merge() function is used to merge one or more arrays into one array.
Tip: You can input one or more arrays to the function.
Note: If two or more array elements have the same key name, the last element overwrites the others.
Note: If you simply input an array to the array_merge() function, and the keys are integers, the function will return a new array with integer keys that are re-indexed starting with 0 (see Example 1 below) .
Tip: The difference between this function and the array_merge_recursive() function is that it handles the case where two or more array elements have the same key name. array_merge_recursive() does not perform key name overwriting, but recursively combines multiple values with the same key name into an array.
array_merge( array1,array2,array3... )
parameter | describe |
---|---|
array1 | Required. Specifies an array. |
array2 | Optional. Specifies an array. |
array3 | Optional. Specifies an array. |
Return value: | Returns the merged array. |
---|---|
PHP version: | 4+ |
Update log: | As of PHP 5.0, this function only accepts parameters of type array. |
Use only one parameter with an integer key name:
<?php $a = array ( 3 => " red " , 4 => " green " ) ; print_r ( array_merge ( $a ) ) ; ?>