Are there specific best practices or algorithms recommended for generating match pairings in PHP, especially for scenarios like a round-robin tournament?

When generating match pairings for scenarios like a round-robin tournament in PHP, it is important to ensure that each team plays against every other team exactly once. One common approach is to use a round-robin scheduling algorithm to create pairings that satisfy this requirement. This algorithm typically involves creating a matrix of pairings where each team plays against every other team once.

<?php
function generateRoundRobinPairings($teams) {
    $numTeams = count($teams);
    if ($numTeams % 2 != 0) {
        array_push($teams, "BYE");
        $numTeams++;
    }

    $pairings = [];
    for ($round = 1; $round < $numTeams; $round++) {
        for ($i = 0; $i < $numTeams / 2; $i++) {
            $team1 = $teams[$i];
            $team2 = $teams[$numTeams - $i - 1];

            if ($team1 != "BYE" && $team2 != "BYE") {
                $pairings[] = [$team1, $team2];
            }
        }

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

    return $pairings;
}

$teams = ["Team A", "Team B", "Team C", "Team D"];
$pairings = generateRoundRobinPairings($teams);

foreach ($pairings as $pairing) {
    echo $pairing[0] . " vs " . $pairing[1] . "\n";
}
?>