Skip to content

Instantly share code, notes, and snippets.

@edgarsandi
Forked from omerida/draw.php
Last active December 30, 2015 09:39
Show Gist options
  • Save edgarsandi/7810512 to your computer and use it in GitHub Desktop.
Save edgarsandi/7810512 to your computer and use it in GitHub Desktop.
(New FIFA rule added)
<?php
/*
* World Cup Draw Example.
*
* Using Array functions & SPL Iterators to simulate (simplified) World Cup draws.
*
* @author Oscar Merida <oscar@phparch.com>
* @author Edgar Sandi <edgar.r.sandi@gmail.com> (New FIFA rule added)
*/
// Teams organized by pots, 8 in each, but the latter group contains 9 teams and the second contains 7 teams,
// a team will be drawn from the last group and added in the second group
$pots = [
['Brazil', 'Argentina', 'Colombia', 'Uruguay', 'Spain', 'Germany', 'Belgium', 'Switzerland'],
['Chile', 'Ecuador', 'Cote d\'Ivoire', 'Ghana', 'Algeria', 'Nigeria', 'Cameroon'],
['Japan', 'Iran', 'Korea Republic', 'Australia', 'USA', 'Mexico', 'Cost Rica', 'Honduras'],
['Netherlands', 'Italy', 'England', 'Portugal', 'Greece', 'Bosnia-Herzegovina', 'Croatia', 'Russia', 'France'],
];
shuffle($pots[3]);
array_push($pots[1], array_pop($pots[3]));
// shuffle teams within each pot and setup the multipleIterator to generate groups
$multiple = new MultipleIterator();
foreach ($pots as $pot) {
shuffle($pot);
$multiple->attachIterator(new ArrayIterator($pot));
}
// $multiple->current() returns an array of 4 teams.
// The first call to current() gives us the first element in each of the 4 pots, and so on.
// Calling next() advances the internal pointer to the next 4 teams in each of the 4 pots.
foreach (range('A', 'H') as $name) {
echo $name . ': ' . implode(', ', $multiple->current()) . PHP_EOL;
$multiple->next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment