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.


Syntax



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 .

Example

<?php
$colors=array("red","black","green");
$colors2=array("yellow","green");
$colors3=array_merge($colors,$colors2);

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

Example

<?php
$x1=array("a"=>"green","b"=>"blue");
$x2=array("c"=>"Pink ,"b"=>"Grey");
print_r(array_merge($x1,$x2));
?>
Run

$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


Related