PHP array_merge()
This array function is used to merge or combine multiple arrays into one new single array. In simple if their are two arrays as array1 and array2 and none of the elements of this two arrays have the same key then using this array_merge function in PHP, we can easily combine both the arrays and it will be displayed as newArray1. and also you can declare this new array in any variable and use it further.
Example: So in the following code we have declared two different arrays with different keys and we have combined them using the array_merge() .
Output :-
Array ( [0] => digital [1] => Technical [2] => Bullshop [3] => Levies [4] => london [5] => brazil [6] => aakash )
PHP array_merge_recursive()
This array function in PHP is a type of function that is used to combine or merge multiple arrays into one single array. In simple, if their are two arrays1 as array2 and array2 and atleast two elements of this two arrays have the same key or values then using this method of array_merge-recursive() function in PHP, we can combine this both the arrays and it will be displayed as newArray2. If you assign only one array to this function then it will act same as that of array_merge().
Example: So in the following code we have declared two arrays in with two of the elements has the same keys and using the array_merge_recursive() we have combined them successfully.
Output :-
Output :-
Array ( [a] => Array ( [0] => digital [1] => london ) [b] => Array ( [0] => Technical [color] => Array ( [0] => green [1] => red [2] => yellow ) ) [c] => Bullshop [d] => Levies [0] => aakash )
Array ( [a] => london [b] => Array ( [color] => Array ( [0] => green [1] => red [2] => yellow ) ) [c] => Bullshop [d] => Levies [0] => aakash )
Difference between array_merge() and array_merge_recursive()
array_merge()
- This function is used when elements of array has different keys
- Syntax: array_merge($array1, $array2, $array3…..and more);
array_merge_recursive()
- This function is used when elements of array has same keys
- Syntax: array_merge_recursive($array1, $array2, $array3….. and more);
I hope you like this information about array function in php. Lot more to come.
Thank You !!