PHP array_merge()Function

PHP Array ReferenceComplete PHP Array Reference Manual

Examples

Merge two arrays into one array:

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

Run Example »

Definition and Usage

The array_merge() function is used to merge one or more arrays into one array.

Tip:You can input one or more arrays into the function.

Note:If two or more array elements have the same key name, the last element will overwrite the other elements.

Note:If you only input one array to the array_merge() function, and the key names are integers, the function will return a new array with integer key names, re-indexed starting from 0 (see Example 1 below).

Tip:This function differs fromarray_merge_recursive()The difference between the functions is in handling the case where two or more array elements have the same key name. array_merge_recursive() does not overwrite key names, but instead recursively combines the values of multiple identical key names into one array.


Syntax

array_merge(array1,array2,array3...)

Parameter Description
array1 Required. Specifies an 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 the array type.


More Examples

Example 1

Only use one parameter with integer key names:

<?php $a=array(3=>"red",4=>"green"); print_r(array_merge($a)); ?>

Run Example »


PHP Array ReferenceComplete PHP Array Reference Manual Other Extensions