Skip to content

Instantly share code, notes, and snippets.

@troyswanson
Created January 2, 2014 20:30
Show Gist options
  • Save troyswanson/8226211 to your computer and use it in GitHub Desktop.
Save troyswanson/8226211 to your computer and use it in GitHub Desktop.
<?php
$teams = array('team 1', 'team 2', 'team 3');
if((count($teams) % 2) == 1) {
array_push($teams, 'bye');
}
$fixed_team = array_slice($teams, 0, 1);
$rotating_teams = array_slice($teams, 1);
$games = array();
//find half minus one offset
$offset = (count($teams)/2)-1;
for($i = 1; $i < count($teams); $i++) {
//slice the teams for first set and put fixed team at beginning of the first set
$set_one = array_merge($fixed_team, array_slice($rotating_teams, 0, $offset));
//create second set of teams
$set_two = array_reverse(array_slice($rotating_teams, $offset));
//create the matchups
for($j = 0; $j < count($teams)/2; $j++) {
$games[$i][$j] = array($set_one[$j], $set_two[$j]);
}
//rotate the teams
array_unshift($rotating_teams, array_pop($rotating_teams));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment