3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sortString($string) { $type_chars = str_split($string); sort($type_chars); return implode($type_chars); } function getValue($map, $input) { $key = implode(',', array_map('sortString', $input)); return $map[$key] ?? null; } function sortMap($map) { return array_reduce(array_keys($map), function ($sorted_map, $types) use ($map) { $sorted_types = preg_replace_callback('/[A-Z]{2}/', function ($matches) { return sortString($matches[0]); }, $types); $sorted_map[$sorted_types] = $map[$types]; return $sorted_map; }, []); } $map = sortMap([ 'CC,GG,AA,CC' => 'high', 'TT,GA,TT,CG' => 'medium', 'CT,AG,TT,GG' => 'low', ]); echo getValue($map, ['CC', 'GG', 'AA', 'CC']) . "\n"; // high echo getValue($map, ['TT', 'AG', 'TT', 'CG']) . "\n"; // medium echo getValue($map, ['TT', 'GA', 'TT', 'GC']) . "\n"; // medium echo getValue($map, ['CT', 'AG', 'TT', 'GG']) . "\n"; // low

preferences:
25.21 ms | 411 KiB | 5 Q