3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); /** * https://stackoverflow.com/a/68884808/367456 */ $arr1 = [ "databases" => [ 0 => [ "id" => 1, "name" => "DB1", "slug" => "db1", "url" => "https://www.db1.org", ], ], ]; $arr2 = [ "databases" => [ 0 => [ "id" => 5, "name" => "DB2", "slug" => "db2", "url" => "https://www.db2.com", ], ], ]; /** * recursively go over two (optional) arrays old and new * while merging them. * * array values are then taken as: * * ['old' => $old, 'new' => $new]. * * @param array|null $old * @param array|null $new * @return array */ function old_and_new(array $old = null, array $new = null): array { $pair = get_defined_vars(); $map = static fn(callable $map, array $arrays): array => in_array(true, array_map('is_array', $arrays), true) && ($parameter = array_combine($k = array_keys($arrays), $k)) && ($keys = array_keys(array_flip(array_merge(...array_values(array_map('array_keys', array_filter($arrays, 'is_array')))))) ) ? array_map( static fn($key) => $map($map, array_map(static fn($p) => $arrays[$p][$key] ?? null, $parameter)), array_combine($keys, $keys) ) : $arrays; return $map($map, $pair); } print_r(old_and_new(new: $arr2));

preferences:
30.82 ms | 414 KiB | 5 Q