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));
Output for 8.4.1 - 8.4.14
Deprecated: old_and_new(): Implicitly marking parameter $old as nullable is deprecated, the explicit nullable type must be used instead in /in/4KdLs on line 41 Deprecated: old_and_new(): Implicitly marking parameter $new as nullable is deprecated, the explicit nullable type must be used instead in /in/4KdLs on line 41 Array ( [databases] => Array ( [0] => Array ( [id] => Array ( [old] => [new] => 5 ) [name] => Array ( [old] => [new] => DB2 ) [slug] => Array ( [old] => [new] => db2 ) [url] => Array ( [old] => [new] => https://www.db2.com ) ) ) )
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.4, 8.3.6 - 8.3.27
Array ( [databases] => Array ( [0] => Array ( [id] => Array ( [old] => [new] => 5 ) [name] => Array ( [old] => [new] => DB2 ) [slug] => Array ( [old] => [new] => db2 ) [url] => Array ( [old] => [new] => https://www.db2.com ) ) ) )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Array ( [databases] => Array ( [0] => Array ( [id] => Array ( [old] => [new] => 5 ) [name] => Array ( [old] => [new] => DB2 ) [slug] => Array ( [old] => [new] => db2 ) [url] => Array ( [old] => [new] => https://www.db2.com ) ) ) )
Output for 7.4.0 - 7.4.33
Parse error: syntax error, unexpected ':' in /in/4KdLs on line 58
Process exited with code 255.
Output for 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'fn' (T_STRING), expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in /in/4KdLs on line 45
Process exited with code 255.

preferences:
161.76 ms | 414 KiB | 5 Q