PHP: array_merge() function

Techno PHP

The array_merge() function is used to merge two or more Array in one. If an index value is matching in arrays (two or more) so at the time of merge array value will be overwritten.
If the input arrays contain numeric keys, the later value will be appended instead of overriding the original value.

In simple word array_merge() function is used for merge two or more array in one array only.


array_merge(array_name1, array_name2, array_name3,array_name4...) 

array_merge- Array function for merge the arrays .

array_name1- Name of the array .(ex- $color1)

array_name2-Name of the array .(ex- $color2)

array_name3-Name of the array .(ex- $color3)

array_name4-Name of the array .(ex- $color4)

array_name N-Name of the N array .(ex-$color N)

N array indicate that you can merge arrays more than two in one array .

A example of array_merge function .



print_r ($colors3);
Let's take an associative array example and merge two arrays $x1 and $x2 .


$x2=array("c"=>"Pink ,"b"=>"Grey");

$x1- Name of the array .

$x2- Name of the array .

"a"=>"green","b"=>"blue" - Associative array (In associative array, we can associate name with each array elements in PHP using => symbol.)

print_r-- print_r is used for print the array in standred array formate .

Learn Array Types.

Index Array .

Two Dimensional Array

Associative Array