Latest web development tutorials

PHP array_merge () function

PHP Array Reference Complete PHP Array Reference

Examples

The two arrays into one array:

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

Running instance »

Definition and Usage

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

Tip: You can enter a function to one or more arrays.

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

NOTE: If you only () function to input an array array_merge, and the key name is an integer, the function will return a new array with integer keys, the keys to re-index 0 (see Example 1 below) .

Tip: This function array_merge_recursive () between different functions in the process two or more of the array elements have the same situation of keys. array_merge_recursive () will not be covering the key name, but multiple values ​​with the same key name recursively composed of an array.


grammar

array_merge( array1,array2,array3... )

参数 描述
array1 必需。规定数组。
array2 可选。规定数组。
array3 可选。规定数组。

technical details

return value: Returns an array of the merger.
PHP version: 4+
Update log: As of PHP 5.0, the function only accepts parameters of type array.


More examples

Example 1

Only use a parameter with integer key names:

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

Running instance »


PHP Array Reference Complete PHP Array Reference