PHP实现世界杯分组球队两两对阵赛程生成方案咨询
Solution to Generate World Cup Group Matches
To generate all unique pairwise matches for each World Cup group (resulting in 6 matches per group of 4 teams), we can use nested loops to avoid duplicate matches (e.g., Team A vs Team B is the same as Team B vs Team A, so we only generate each pair once).
Here's a complete PHP implementation using your provided arrays:
<?php $worldCupTeams = array( '1' => array('name' => 'Russia', 'group' => 'A'), '2' => array('name' => 'Saudi Arabia', 'group' => 'A'), '3' => array('name' => 'Egypt', 'group' => 'A'), '4' => array('name' => 'Uruguay', 'group' => 'A'), '5' => array('name' => 'Portugal', 'group' => 'B'), '6' => array('name' => 'Spain', 'group' => 'B'), '7' => array('name' => 'Morocco', 'group' => 'B'), '8' => array('name' => 'Iran', 'group' => 'B'), '9' => array('name' => 'France', 'group' => 'C'), '10' => array('name' => 'Australia', 'group' => 'C'), '11' => array('name' => 'Peru', 'group' => 'C'), '12' => array('name' => 'Denmark', 'group' => 'C'), '13' => array('name' => 'Argentina', 'group' => 'D'), '14' => array('name' => 'Iceland', 'group' => 'D'), '15' => array('name' => 'Croatia', 'group' => 'D'), '16' => array('name' => 'Nigeria', 'group' => 'D'), '17' => array('name' => 'Brazil', 'group' => 'E'), '18' => array('name' => 'Switzerland', 'group' => 'E'), '19' => array('name' => 'Costa Rica', 'group' => 'E'), '20' => array('name' => 'Serbia', 'group' => 'E'), '21' => array('name' => 'Germany', 'group' => 'F'), '22' => array('name' => 'Mexico', 'group' => 'F'), '23' => array('name' => 'Sweden', 'group' => 'F'), '24' => array('name' => 'South Korea', 'group' => 'F'), '25' => array('name' => 'Belgium', 'group' => 'G'), '26' => array('name' => 'Panama', 'group' => 'G'), '27' => array('name' => 'Tunisia', 'group' => 'G'), '28' => array('name' => 'England', 'group' => 'G'), '29' => array('name' => 'Poland', 'group' => 'H'), '30' => array('name' => 'Senegal', 'group' => 'H'), '31' => array('name' => 'Colombia', 'group' => 'H'), '32' => array('name' => 'Japan', 'group' => 'H') ); $worldCupGroups = array( 'A' => array(1, 2, 3, 4), 'B' => array(5, 6, 7, 8), 'C' => array(9, 10, 11, 12), 'D' => array(13, 14, 15, 16), 'E' => array(17, 18, 19, 20), 'F' => array(21, 22, 23, 24), 'G' => array(25, 26, 27, 28), 'H' => array(29, 30, 31, 32) ); // Initialize an array to store all group matches (optional, if you need to reuse the data) $allGroupMatches = []; // Iterate over each group foreach ($worldCupGroups as $groupName => $teamIds) { $groupMatches = []; $numTeams = count($teamIds); // Generate unique pairwise matches for ($i = 0; $i < $numTeams; $i++) { // Get the first team in the pair $team1 = $worldCupTeams[$teamIds[$i]]['name']; // Loop through teams that come after the current one to avoid duplicates for ($j = $i + 1; $j < $numTeams; $j++) { // Get the second team in the pair $team2 = $worldCupTeams[$teamIds[$j]]['name']; // Store the match (or output directly) $match = "$team1 vs $team2"; $groupMatches[] = $match; // Optional: Echo the match immediately echo "$groupName Group: $match<br>"; } } // Add the group's matches to the overall array $allGroupMatches[$groupName] = $groupMatches; } // If you need to use the matches later, $allGroupMatches contains all groups' matches // print_r($allGroupMatches); ?>
How This Works:
- Loop Through Groups: We first iterate over each group in
$worldCupGroupsto process one group at a time. - Avoid Duplicate Matches: Using nested loops where the inner loop starts at
$i + 1ensures we only generate each pair once (no reverse matches like "Saudi Arabia vs Russia" after "Russia vs Saudi Arabia"). - Lookup Team Names: For each team ID in the group, we fetch the corresponding team name from
$worldCupTeams. - Store/Output Matches: You can either store the matches in an array (
$allGroupMatches) for later use or echo them immediately as they're generated.
This approach guarantees exactly 6 matches per group (since for 4 teams, the number of unique pairs is 4 * 3 / 2 = 6), which aligns with World Cup group stage rules.
内容的提问来源于stack exchange,提问作者David
相关产品推荐
相关产品推荐

