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 = ['a' => [1,2,3,4]]; print_r(arrayMergeRecursiveDistinct($b, $a)); print_r(array_replace_recursive($b, $a));

preferences:
44.32 ms | 402 KiB | 5 Q