Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using PHP 5.5 Generators to simulate an array with duplicate keys
function duplicateKeys($string) {
$string = strtolower($string);
$length = strlen($string);
for ($i = 0; $i < $length; ++$i) {
yield ord(strpos($string, $string[$i])) => $string[$i];
}
}
foreach (duplicateKeys('badass') as $i => $value) {
echo $i , ' -> ' , $value, PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.