Skip to content

Instantly share code, notes, and snippets.

@MarkBaker
Created July 20, 2013 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkBaker/6046842 to your computer and use it in GitHub Desktop.
Save MarkBaker/6046842 to your computer and use it in GitHub Desktop.
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