我有一个阵列......
$teams=array('team_one','team_two','team_three');
我正在尝试创建一个(非常基本的)灯具列表。我正在寻找的输出可以在下面看到......
team_one v team_two
team_one v team_three
team_two v team_one
team_two v team_three
team_three v team_one
team_three v team_two
我拥有的代码,显然只适用于第一个team_one灯具......
$count=count($teams)-1;
for ($i=0;$i<$count;$i++) {
$opposition=$i+1;
echo "$teams[0] v $teams[$opposition] <br>";
}
我无法理解的是如何再次循环,但现在需要从team_two开始,并且能够循环回到数组,以便team_one不会错过在第二轮比赛中,以及第三轮比赛中的team_one和team_two。
如果有更好的方法(我确定有),我真的很感激帮助。
答案 0 :(得分:6)
这个怎么样:
foreach($teams AS $team) {
foreach($teams AS $opposition) {
if($team != $opposition) {
echo "$team v $opposition <br>";
}
}
}
答案 1 :(得分:1)
你应该使用tow进行循环:
$count = count($teams);
for ($i = 0; $i < $count; $i++) {
for($j = 0; $j < $count; $j++) {
if ($i == $j) continue;
echo $teams[$i] . " vs ". $teams[$j] . "\n";
}
}
答案 2 :(得分:1)
$teams=array('team_one','team_two','team_three');
$l = count($teams);
for($i=0;$i <$l;$i++){
$t2 = $teams[$i];
foreach($teams as $t){
if($t != $t2 )
echo $t ." VS ". $t2. "\n";
}
}