skip to Main Content

I have a problem. Think of a football league and there are 16 teams in this football league.

$teams = array(
"Team1", "Team2", "Team3", "Team4",
"Team5", "Team6", "Team7", "Team8",
"Team9", "Team10", "Team11", " Team12",
"Team13", "Team14", "Team15", "Team16"
);

Each team can only play one match against each other and each team can only play one match per week.

With 8 matches per week, this makes 120 matches in total.

Some of these matches have been played before.

The matches of the 1st week and the 2nd week are as follows:

1 week

Team14 vs Team9,
Team1 vs Team10,
Team13 vs Team3,
Team11 vs Team5,
Team2 vs Team6,
Team4 vs Team15,
Team16 vs Team12,
Team7 vs Team8

2 weeks

Team6 vs Team10,
Team13 vs Team2,
Team9 vs Team8,
Team14 vs Team1,
Team3 vs Team4,
Team15 vs Team7,
Team16 vs Team5,
Team11 vs Team12

How can I find the remaining matches of the fixture so that the matches that have been played or will be played do not repeat?

Even though I tried many times with PHP, I could not succeed.

2

Answers


  1. In your case, I would start by creating a table with all the possible occurences.

    I would create a unique key for each match, e.g. Team1_Team2 for Team1 vs Team2 and so on.

    Using a key can be easier to compare afterwards if a match has already been played.

    To create the occurrences, we use a nested loop.
    The inner loop starts at 1 because you can’t play the same team twice.

    Note: An error has crept in on Team12 – there’s one space too many.

    $teams = array(
        "Team1", "Team2", "Team3", "Team4",
        "Team5", "Team6", "Team7", "Team8",
        "Team9", "Team10", "Team11", "Team12",
        "Team13", "Team14", "Team15", "Team16"
    );
        
    // Can't play the same team twice
    $i = 1;
        
    $occurences = [];
        
     foreach ($teams as $team) {
        for ($j = $i; $j < count($teams); $j++) {
            $keyMatch = $team . '_' . $teams[$j]; 
            $occurences[$keyMatch] = $team . ' vs ' . $teams[$j]; 
        }
        $i++;
    }
        
    // print_r($occurences); array('Team1_Team2' => 'Team1 vs Team2', 'Team1_Team3' => 'Team1 vs Team3' ...
    

    We have all the occurrences, each key always has the team with the lowest number to the left of the "_".

    For each match already played, we now need to create the key corresponding to the match and compare it with our occurences.

    As you can see in your example matches: Team14 vs Team9.
    The weakest team is not necessarily on the left.
    We therefore need to sort before creating the key:

    // example with only 2 of the values
    $matchsWeek = ['Team14 vs Team9', 'Team1 vs Team10'];
    
    foreach ($matchsWeek as $match) {
        // To remove the vs and put each team in a different table index.
        $teamsMatch = explode(' vs ', $match);
        
        // This will produce an array with only the team number.
        // Remove 'Team' and convert to int.
        $teamsId = array_map(function($team) { 
            return intval(str_replace('Team', '', $team));
        }, $teamsMatch);
        
        // sort ASC
        sort($teamsId);
        
        // create key
        $key = sprintf("Team%s_Team%s", $teamsId[0], $teamsId[1]);
        
        // if key is found, delete occurence
        if (isset($occurences[$key])) {
            unset($occurences[$key]);
        }
    }
    

    The 2 matches have been deleted and $occurences now only has 118 matches.

    I hope I’ve been able to help you.

    Login or Signup to reply.
  2. there can be more optimal solutions for this problem but here is a approach i think you can use to solve this problem

    Generate the Complete Round-Robin Schedule where each team plays against every other team exactly once we can use Rotation MEchanism that will make sure that every team plays against every other team.

    Remove Played Matches

    Output the Remaining Schedule

    you can do something like this

    <?php
    
    function rotateTeams($teams) {
        $item = array_splice($teams, 1, 1); // Remove the second team
        array_push($teams, array_shift($item)); // Add it before the last team
    }
    
    function generateSchedule($teams) {
        $schedule = [];
        $teamsCount = count($teams);
        $rounds = $teamsCount - 1;
        $matchesPerRound = $teamsCount / 2;
    
        for ($i = 0; $i < $rounds; $i++) {
            for ($j = 0; $j < $matchesPerRound; $j++) {
                $home = $teams[$j];
                $away = $teams[$teamsCount - 1 - $j];
                $schedule[$i][] = [$home, $away];
            }
            rotateTeams($teams);
        }
    
        return $schedule;
    }
    
    function filterPlayedMatches($schedule, $playedMatches) {
        foreach ($schedule as $round => &$matches) {
            foreach ($matches as $key => $match) {
                if (in_array($match, $playedMatches) || in_array(array_reverse($match), $playedMatches)) {
                    unset($matches[$key]);
                }
            }
            $matches = array_values($matches); // Re-index array
        }
    
        return array_filter($schedule); // Remove empty rounds
    }
    
    // Teams array
    $teams = ["Team1", "Team2", "Team3", "Team4", "Team5", "Team6", "Team7", "Team8", "Team9", "Team10", "Team11", "Team12", "Team13", "Team14", "Team15", "Team16"];
    
    // Matches already played
    $playedMatches = [
        ["Team14", "Team9"], ["Team1", "Team10"], ["Team13", "Team3"], ["Team11", "Team5"],
        ["Team2", "Team6"], ["Team4", "Team15"], ["Team16", "Team12"], ["Team7", "Team8"],
        ["Team6", "Team10"], ["Team13", "Team2"], ["Team9", "Team8"], ["Team14", "Team1"],
        ["Team3", "Team4"], ["Team15", "Team7"], ["Team16", "Team5"], ["Team11", "Team12"]
    ];
    
    // Generate full schedule
    $fullSchedule = generateSchedule($teams);
    
    // Filter out played matches
    $remainingSchedule = filterPlayedMatches($fullSchedule, $playedMatches);
    
    // Output the remaining schedule
    echo "Remaining Matches:<br>";
    foreach ($remainingSchedule as $week => $matches) {
        echo "Week " . ($week + 1) . ":<br>";
        foreach ($matches as $match) {
            echo $match[0] . " vs " . $match[1] . "<br>";
        }
        echo "<br>";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search