【发布时间】:2011-04-20 12:49:28
【问题描述】:
$teams = array(1, 2, 3, 4, 5, 6, 7, 8);
$game1 = array(2, 4, 6, 8);
$game2 = array();
如果teams[x] 不在game1 中,则插入game2
for($i = 0; $i < count($teams); $i++){
for($j = 0; $j < count($game1); $j++){
if($teams[$i] == $game1[$j]){
break;
} else {
array_push($game2, $teams[$i]);
}
}
}
for ($i = 0; $i < count($game2); $i++) {
echo $game2[$i];
echo ", ";
}
我希望结果是:
1, 3, 5, 7,
但是,我得到:
1, 1, 1, 1, 3, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 8,
我该如何改进呢?谢谢
【问题讨论】:
标签: php arrays loops for-loop nested-loops