Skip to content

Instantly share code, notes, and snippets.

@makkinga
Created December 5, 2022 14:26
Show Gist options
  • Save makkinga/a250f8dbc05ea30f7ded25c62c485119 to your computer and use it in GitHub Desktop.
Save makkinga/a250f8dbc05ea30f7ded25c62c485119 to your computer and use it in GitHub Desktop.
Draw lots for your Christmas surprises!
<?php
/*
* LotDrawer::draw([
* 'Frank' => '[email protected]',
* 'John' => '[email protected]',
* 'Nathan' => '[email protected]',
* 'Jane' => '[email protected]',
* ]);
*/
class LotDrawer
{
/**
* Draw lots
*
* @param array $names
* @return array
*/
public static function draw(array $names): array
{
$shuffled = array_keys($names);
shuffle($shuffled);
$result = [];
$index = 0;
foreach ($names as $name => $email) {
if ($shuffled[$index] === $name) {
return self::draw($names);
}
$result[] = [
'name' => $name,
'email' => $email,
'assigned_to' => $shuffled[$index],
];
$index++;
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment