Skip to content

Instantly share code, notes, and snippets.

@paulmallet
Forked from damienalexandre/santa.php
Last active August 29, 2015 14:08
Show Gist options
  • Save paulmallet/3221afc9ccf0ccbbfb85 to your computer and use it in GitHub Desktop.
Save paulmallet/3221afc9ccf0ccbbfb85 to your computer and use it in GitHub Desktop.
<?php
/**
* Secret Santa Generator - powered by PHP Generators for the lolz.
*/
$players = [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
];
function giveMeABuddy($players) {
// Make sure results are not predictable
shuffle($players);
foreach ($players as $key => $player) {
// each player is the buddy of the last one
$santa = $players[($key+1) % count($players)];
yield $player => $santa;
}
}
foreach (giveMeABuddy($players) as $player => $santa)
{
printf("%s is the santa of %s\n", $santa, $player);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment