How can the Rutschsystem algorithm be implemented in PHP for creating a league schedule?

The Rutschsystem algorithm can be implemented in PHP for creating a league schedule by using a combination of round-robin scheduling and randomization to ensure fairness and variety in match-ups. This algorithm helps to evenly distribute the games among teams while avoiding repetitive pairings.

<?php

function generateLeagueSchedule($teams) {
    $numTeams = count($teams);
    $schedule = array();

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

    for ($i = 0; $i < $numTeams - 1; $i++) {
        shuffle($teams);
        $midpoint = $numTeams / 2;
        $firstHalf = array_slice($teams, 0, $midpoint);
        $secondHalf = array_slice($teams, $midpoint);

        for ($j = 0; $j < $midpoint; $j++) {
            $match = array($firstHalf[$j], $secondHalf[$j]);
            $schedule[] = $match;
        }

        array_push($teams, array_shift(array_splice($teams, 1, 1)));
    }

    return $schedule;
}

// Example usage
$teams = array("Team A", "Team B", "Team C", "Team D");
$schedule = generateLeagueSchedule($teams);

foreach ($schedule as $match) {
    echo $match[0] . " vs " . $match[1] . "\n";
}

?>