What are some best practices for efficiently creating a game schedule for an odd number of teams in PHP?

When creating a game schedule for an odd number of teams in PHP, a common approach is to introduce a "bye" team that sits out each round. This ensures that every team has a game to play in each round. To efficiently create a schedule, you can use a round-robin algorithm to generate matchups for each round, taking into account the bye team.

<?php

// Function to generate a game schedule for an odd number of teams
function generateSchedule($teams) {
    $numTeams = count($teams);

    if ($numTeams % 2 != 0) {
        array_push($teams, 'Bye');
        $numTeams++;
    }

    $schedule = [];

    for ($i = 0; $i < $numTeams - 1; $i++) {
        for ($j = 0; $j < $numTeams / 2; $j++) {
            $matchup = [$teams[$j], $teams[$numTeams - $j - 1]];
            $schedule[] = $matchup;
        }

        array_splice($teams, 1, 0, array_pop($teams));
    }

    return $schedule;
}

// Example usage
$teams = ['Team A', 'Team B', 'Team C', 'Team D', 'Team E'];
$schedule = generateSchedule($teams);

foreach ($schedule as $round => $matchups) {
    echo "Round " . ($round + 1) . ":\n";
    foreach ($matchups as $matchup) {
        echo $matchup[0] . " vs " . $matchup[1] . "\n";
    }
    echo "\n";
}