3v4l.org

run code in 300+ PHP versions simultaneously
<?php function arrayMergeRecursiveDistinct(array $first, array $second) { foreach ($second as $idx => $value) { if (is_integer($idx)) { $first[] = $value; } else { if (!array_key_exists($idx, $first)) { $first[$idx] = $value; } else { if (is_array($value)) { $first[$idx] = arrayMergeRecursiveDistinct($first[$idx], $value); } else { $first[$idx] = $value; } } } } return $first; } $a = ['a' => [1,2,3], 'b' => ['c' => ['d' => 'e']]]; $b = ['a' => [2,3,4], 'b']; print_r(arrayMergeRecursiveDistinct($b, $a)); print_r(array_replace_recursive($b, $a));

preferences:
39.05 ms | 402 KiB | 5 Q