Php 夹具生成器代码

Php 夹具生成器代码,php,Php,这是我的密码: $totalRounds = 1; $teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4', 'Team 5'); echo 'Total Teams: ' , $totalTeams = count($teams) , '<br/>'; $turns = $totalTeams; for($round=1; $round<$totalRounds+1; $round++){ echo 'Round:

这是我的密码:

$totalRounds = 1;
$teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4', 'Team 5');
echo 'Total Teams: ' , $totalTeams = count($teams) , '<br/>';
$turns = $totalTeams;

for($round=1; $round<$totalRounds+1; $round++){
    echo 'Round: ' , $round , '<br/>';

    for($homeTeam=0; $homeTeam<$totalTeams-1; $homeTeam++){
        for($awayTeam=0; $awayTeam<$totalTeams; $awayTeam++){
            if($teams[$homeTeam] != $teams[$awayTeam]){
                echo $teams[$homeTeam] , ' v/s ' , $teams[$awayTeam] , '<br/>';
            }                        
        }
        unset($teams[$homeTeam]);
    }
    echo '<br/>';
}
$totalRounds=1;
$teams=数组('team1','team2','team3','team4','team5');
echo“团队总数:”,$totalTeams=count($Teams),“
”; $turns=$totalTeams;
对于($round=1;$round您的代码正在销毁数组,例如:

first iteration: $home = 0; $away = 0
end of iteration: delete teams[0]

second iteration, $home = 1; $away = 0 - OOPS, teams[0] no longer exists
不应在运行时取消设置数组,而应将内循环建立在外循环的基础上,例如:

for($home = 0; ...) {
   for($away = $home + 1; ...) {

您可以使用以下方法修复代码

$totalRounds = 1;
$teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4', 'Team 5');
echo 'Total Teams: ' , $totalTeams = count($teams) , '<br/>';
$turns = $totalTeams;

for($round=1; $round<$totalRounds+1; $round++){
    echo 'Round: ' , $round , '<br/>';

    for($homeTeam = 0; $homeTeam < $totalTeams - 1; $homeTeam++) {
        for($awayTeam=$homeTeam + 1; $awayTeam < $totalTeams; $awayTeam++) {
            echo $teams[$homeTeam] , ' v/s ' , $teams[$awayTeam] , '<br/>';
        }
    }
    echo '<br/>';
}
$totalRounds=1;
$teams=数组('team1','team2','team3','team4','team5');
echo“团队总数:”,$totalTeams=count($Teams),“
”; $turns=$totalTeams;
对于($round=1;$round感谢Marc&Philipp提供的解决方案。如何为每支球队提供两场主场比赛和两场客场比赛?
$totalRounds = 1;
$teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4', 'Team 5');
echo 'Total Teams: ' , $totalTeams = count($teams) , '<br/>';
$turns = $totalTeams;

for($round=1; $round<$totalRounds+1; $round++){
    echo 'Round: ' , $round , '<br/>';

    for($homeTeam = 0; $homeTeam < $totalTeams - 1; $homeTeam++) {
        for($awayTeam=$homeTeam + 1; $awayTeam < $totalTeams; $awayTeam++) {
            echo $teams[$homeTeam] , ' v/s ' , $teams[$awayTeam] , '<br/>';
        }
    }
    echo '<br/>';
}