Skip to content

Instantly share code, notes, and snippets.

@damienalexandre
Created October 31, 2014 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save damienalexandre/9483de44cf1219a74973 to your computer and use it in GitHub Desktop.
Save damienalexandre/9483de44cf1219a74973 to your computer and use it in GitHub Desktop.
Secret Santa matcher with PHP Generator
<?php
/**
* Secret Santa Generator - powered by PHP Generators for the lolz.
*/
$players = [
"xx@jolicode.com",
"spam@jolicode.com",
"nop@jolicode.com",
"foo@jolicode.com",
];
function giveMeABuddy($players) {
// Make sure results are not predictable
shuffle($players);
$players = array_values($players);
foreach ($players as $key => $player) {
$santa = $players[(count($players)-1) - $key];
yield $player => $santa;
}
}
foreach (giveMeABuddy($players) as $player => $santa)
{
printf("%s is the santa of %s\n", $santa, $player);
}
@paulmallet
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment