Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP array_merge() Function

PHP array_merge() Function

PHP Array ReferenceComplete PHP Array Reference Manual

Example

Merge two arrays into one array:

" red " , " b " => " green " ) ; $a2 = array ( " c " => " blue " , " b " => " yellow " ) ; print_r ( array_merge ( $a1 , $a2 ) ) ; ?>

Run Example »


Definition and Usage

The array_merge() function merges one or more arrays into one array.

**Tip:** You can pass one or more arrays to the function.

**Note:** If two or more array elements have the same key, the last element overwrites the others.

**Note:** If you pass only one array to array_merge() and the keys are integers, the function returns a new array with integer keys reindexed starting from 0 (see Example 1 below).

**Tip:** The difference between this function and array_merge_recursive() is how it handles two or more array elements with the same key. array_merge_recursive() does not overwrite keys; instead, it recursively combines the values of identical keys into an array.


Syntax

array_merge(

array1,array2,array3...

)

Parameters Description
array1 Required. Specifies the array.
array2 Optional. Specifies an array.
array3 Optional. Specifies an array.

Technical Details

Return Value: Returns the merged array.
PHP Version: 4+
Changelog: As of PHP 5.0, this function only accepts parameters of type array.

More examples

Example 1

Using only one parameter with integer keys:

" red " , 4 => " green " ) ; print_r ( array_merge ( $a ) ) ; ?>

Run Example »


PHP Array ReferenceComplete PHP Array Reference Manual