3v4l.org

run code in 300+ PHP versions simultaneously
<?php function arrayMergeRecursive($arraySource, $arrayDestination) { $temp = array(); foreach ($arraySource as $key => $value) { if (is_array($value)) { $associatedDestination = array(); if (isset($arrayDestination[$key])) { $associatedDestination = $arrayDestination[$key]; } $temp[$key] = arrayMergeRecursive($value, $associatedDestination); continue; } if (isset($arrayDestination[$key])) { $temp[$key] = $arrayDestination[$key]; } else { $temp[$key] = $arraySource[$key]; } } return $temp; } $source = ["a" => 1, "b" => 2, "c" => ["x" => 1, "y" => 2]]; $x = ["c" => 1, "d" => 10, "a" => 5]; var_dump(arrayMergeRecursive($source, $x)); var_dump(array_replace_recursive($source, $x));

preferences:
58.49 ms | 402 KiB | 5 Q