array_merge_recursive() merges arrays recursively. When the same string key exists in more than one input array, the colliding values are combined rather than simply overwritten.
$a = ['settings' => ['mode' => 'light']];
$b = ['settings' => ['mode' => 'dark']];
$result = array_merge_recursive($a, $b);
print_r($result);For configuration overrides, array_replace_recursive() may be a better fit when later values should replace earlier values instead of being combined.
array_merge_recursive ( $array1, $array1 ....)
$array1 : Required , Input array 1 . $input1=array(1,2,3,4);
$input2=array(5,6,7);
echo print_r(array_merge_recursive($input1,$input2));
Output is here.
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) 1
$input1=array("one"=>'first','Second'=>'2nd',3,4);
$input2=array("Five"=>'5th',6,7);
print_r(array_merge_recursive($input1,$input2));
Output is here.
Array ( [one] => first [Second] => 2nd [0] => 3 [1] => 4 [Five] => 5th [2] => 6 [3] => 7 )
$array1 = ['color' => 'red'];
$array2 = ['color' => 'blue'];
$result = array_merge_recursive($array1, $array2);
print_r($result); // Output: ['color' => ['red', 'blue']]
$array1 = ['info' => ['name' => 'John', 'age' => 30]];
$array2 = ['info' => ['age' => 40, 'city' => 'New York']];
$result = array_merge_recursive($array1, $array2);
print_r($result); // Output: ['info' => ['name' => 'John', 'age' => [30, 40], 'city' => 'New York']]
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.