Skip to content

Instantly share code, notes, and snippets.

@darraghenright
Created December 1, 2013 23:19
Show Gist options
  • Save darraghenright/7742157 to your computer and use it in GitHub Desktop.
Save darraghenright/7742157 to your computer and use it in GitHub Desktop.
<?php
$names = ['Fred', 'Wilma', 'Pebbles', 'Barney', 'Betty', 'Bamm-Bamm'];
$count = count($names);
$count % 2 === 0 || exit('Error! You need an even number of names.');
shuffle($names);
$a = array_slice($names, 0, $count / 2);
$b = array_slice($names, $count / 2, $count);
$match = function($givers, $receivers)
{
shuffle($receivers);
return array_map(function($giver, $receiver) {
return sprintf('%s buys for %s', $giver, $receiver);
}, $givers, $receivers);
};
echo implode(PHP_EOL, array_merge($match($a, $b), $match($b, $a)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment