The 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.
Array - Array is a collection of similar data items.
Learn more about arrays in PHP.
Merge - The merge word is taken from the English word, which means that you are merging(adding) two or more things together in one.
In another way, you can say that the Array merge function is used to convert two or more arrays to an array.
Definition of the Array merge function
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 index array contain numeric keys, the later value will be appended instead of overriding the original value.
Syntax of the Array merge function
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 .
array2-Second Array
array3-Third Array .
array4-Fourth Array.
Follow this syntax and create examples.
Examples of the Array merge function
In the following example,we have created two arrays and merge both arrays in one.
Example
<?php
$colors=array("red","black","green");
$colors2=array("yellow","green");
$colors3=array_merge($colors,$colors2);
print_r ($colors3);
?>
Run
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.
Associative arrays merge
In the previous tutorial, we have discussed about associative arrays. Array () function is create Arrays.
You can to also create an associated array and merge them with the help of an 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.
Example
<?php
$x1=array("a"=>"green","b"=>"blue");
$x2=array("c"=>"Pink ,"b"=>"Grey");
print_r(array_merge($x1,$x2));
?>
Run
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 .
Overwriting values
Array merge function overwrites the key values. if the second array has the same key value as the first array, then the array merge function overwrites the value by merge both arrays. Let's understand with an example.
Example
<?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);
?>
Output-
Array ( [0] => 12 [1] => 23 [2] => 34 [3] => 45 [4] => 45 [5] => 56 [6] => 67 [7] => 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. The Array merge function is overwritten both of these same values.
Combined array without overwriting
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.
Example
<?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);
?>
Output
Array ( [0] => 12 [1] => 23 [2] => 34 [3] => 45 [4] => 56 [5] => 67 [6] => 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 .
Learn types of Array
Index Array .
Two Dimensional Array
Associative Array
Recommended Posts:-