Array merge function is used to merge two or more arrays in one. In the previous tutorial, we have discussed arrays, Index Arrays, two-dimensional arrays. and associative arrays. If you do not know about Array then you will not understand Array Merge function. You should know about complete arrays, then you will be able to understand the array merge function. Array merge is made up of two words.
1Array - Array is a collection of similar data items. Learn more about arrays in PHP.
Merge - The merge word is taken from the English word, means that you are merging(adding) two or more things together in one. In another way, you can say that Array merge function is used to convert two or more arrays to an array.
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.
The following syntax will help you understand the concept of array merge function in PHP .
array_merge(array1, array2, array3,array4...)
In the above syntax, you can see how many arrays have been passed under the array merge function.
Any arrays that are passed under the Array merge function are merged into an array.
array_merge- Array function for merge the arrays .
array1-First Array .
array3-Third Array .
Follow this syntax and create examples.
In the following example,we have create two arrays and merge both array in one.
<?php $colors=array("red","black","green"); $colors2=array("yellow","green"); $colors3=array_merge($colors,$colors2); print_r ($colors3); ?>
In the above example,we created two arrays. The items of the first array are red, black, green. And the other array items are yellow and green. By using the Array function, we create an array. After creating an array, using Array merge function, the two arrays are merged into one array.
In the previous tutorial, we have discussed about associative arrays. Array () function is create Arrays. You can also create an associate array and merge them with the help of array merge function. In the given example, we will merge them into an array by creating two associate arrays. The following example helps you understand the associative arrays merge.
<?php $x1=array("a"=>"green","b"=>"blue"); $x2=array("c"=>"Pink ,"b"=>"Grey"); print_r(array_merge($x1,$x2)); ?>
In this above example, we have created two associate arrays.
After created arrays, we have merged those two arrays with the help of array merge function.
$x1- Variable of First Array.
$x2- Variable of Second Array .
"a"=>"green","b"=>"blue" - Associative array (In associative array,information store in the key-value pair)
print_r-- print_r() function is used to print the array in standred array formate .
<?php $arr1=array( '12' => '12', '23' => '23', '34' => '34', '45' => '45' ); $arr2=array( '45' => '45', '56' => '56', '67' => '67', '78' => '78' ); $result = array_merge($arr1, $arr2); print_r($result); ?>
Array (  => 12  => 23  => 34  => 45  => 45  => 56  => 67  => 78 )
In this above example, the first array has 45=>45 key and value and in the second array, as like first array 45=>45. Array merge function is overwritten both of these same values.
Before this, we have already seen the overwritten behavior of Array Merge function in the example. In this example, we use the + operator to implement such array_unique.
<?php $arr1=array( '12' => '12', '23' => '23', '34' => '34', '45' => '45' ); $arr2=array( '45' => '45', '56' => '56', '67' => '67', '78' => '78' ); $result = array_merge($arr1+$arr2); print_r($result); ?>
Array (  => 12  => 23  => 34  => 45  => 56  => 67  => 78 )
In this above example, the first array has the key=>value 45=>45 and the second array has the same key=>value 45 =>45. Using Array Merge Function and + Operator, we can remove duplicacy .
Index Array .
Two Dimensional Array