UltraMega Blog
4May/102

Round Robin Tournament Scheduler

Here's a snippet I wrote for a project that I thought might come in handy for someone else. It generates a schedule for a round robin tournament. I found some other snippets to do this, but I found them overly complicated or lacking in other ways.

Here's an example of what this code does:

< ?php
$players = range(1, 8);
 
echo 'Original List: ' . PHP_EOL;
print_r($players);
echo PHP_EOL;
echo 'Round Robin Schedule: ' . PHP_EOL;
print_r(generateRRSchedule($players));
?>

Output:

Original List: 
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
)
 
Round Robin Schedule: 
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 1
                    [1] => 8
                )
 
            [1] => Array
                (
                    [0] => 2
                    [1] => 7
                )
 
            [2] => Array
                (
                    [0] => 3
                    [1] => 6
                )
 
            [3] => Array
                (
                    [0] => 4
                    [1] => 5
                )
 
        )
 
(...)
 
)