PHP array_merge() function

Sample

Ihahalawak ang dalawang array upang maging isang array:

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

Run Example

Pagsasaayos at paggamit

Ang function na array_merge() ay magkakasama ng isang o ilang array upang maging isang array.

Tip:Maaaring ipasok sa function ang isang o ilang array.

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

Comment:If you input only one array to the array_merge() function and the key name is an integer, then the function will return a new array with integer keys, which are reindexed starting from 0 (see the example below 1).

Tip:This function is similar to array_merge_recursive() The difference between 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 recursively forms an array of values with the same key name.

Syntax

array_merge(array1,array2,array3...)
Parameter Description
array1 Required. Specify array.
array2 Optional. Specify array.
array3 Optional. Specify array.

Technical Details

Return Value: Return the merged array.
PHP Version: 4+
Update Log: Starting from PHP 5.0, this function only accepts array type parameters.

More Examples

Example 1

Merge two associative 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

Example 2

Use only one array parameter with integer keys:

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

Run Example