3v4l.org

run code in 500+ PHP versions simultaneously
<?php function print_line(string $output): void { echo $output . PHP_EOL; } function normalize_property_keys(array $props, ?array $matchers = []): array { $normalized = []; foreach ($props as $key => $value) { $matched_key = $matchers[$key] ?? null; if ($matched_key) { $normalized[$matched_key] = $value; } else { $norm_key = preg_replace('/[-\s]+/', '', $key); $normalized[$norm_key] = $value; } } return $normalized; } $keys = [ 'align' => 'alignment', 'col' => 'color', 'colour' => 'color', ]; $tests = [ 'align' => 1, 'col' => 2, 'test-key' => 3, 'test_new_key' => 4, ]; print_line("Original Array"); print_r($tests); print_line(""); print_line("Normalized Array"); print_r(normalize_property_keys($tests, $keys));
Output for 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
Original Array Array ( [align] => 1 [col] => 2 [test-key] => 3 [test_new_key] => 4 ) Normalized Array Array ( [alignment] => 1 [color] => 2 [testkey] => 3 [test_new_key] => 4 )

preferences:
24.14 ms | 712 KiB | 4 Q