【发布时间】:2021-03-25 09:33:00
【问题描述】:
我正在 Laravel 中制作匹配生成器。我有一个名为 $matches 的数组。问题是,当我输出代码时,每个团队都打了两次。我使用的代码如下所示。
public function generateMatches($allContestants){
$matches = [];
for ($i=0; $i < count($allContestants) -1; $i++) {
$matches[] = [
'team1' => $allContestants[$i]->id,
'team2' => $allContestants[$i+1]->id
];
}
dd($matches);
}
这是我得到的输出:
array:3 [▼
0 => array:2 [▼
"team1" => "1"
"team2" => "10"
]
1 => array:2 [▼
"team1" => "10"
"team2" => "11"
]
2 => array:2 [▼
"team1" => "11"
"team2" => "12"
]
]
如您所见,每场比赛的第二支球队是下一场比赛中使用的第一支球队。有谁知道如何解决这个问题?
【问题讨论】: