The array_merge_recursive function merges/combines one or more arrays into one array.
The array_merge_recursive function is different from the array_merge in a use case that is when there is the repetition of keys in two or more arrays, instead of overriding the value of the key, it creates a new array using the value.
Upon assigning only a single array to the array_merge_recursive function, it behaves exactly like the array_merge function.
What is the syntax of the array_merge_recursive function in PHP?
array_merge_recursive(array1, array2, array3, ...)
Parameter Values
Parameter | Description |
---|---|
array1 | Specify the array to be merged – Required |
array2 | Specify the array to be merged – Optional |
array3,… | Specify the array to be merged – Optional |
Example of array_merge_recursive function
<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge_recursive($a1,$a2));
?>
You must check the output of the above example and try modifying the example by keeping the keys the same in two or more arrays with the same and different values.