我有这个相反的代码。
假设我有 1,2,3,3,4,5 的数组
while ($row = mysql_fetch_assoc($result)) {
$item = $row['item'];
echo $item . '<br />'; // for testing
$items[] = $row['item'];
if (!in_array($item, $items)) {
$output[] = $row;
foreach ($items as $item) {
echo 'Array thus far: ' . $item . '<br />'; // for testing
}
}
}
基本上我希望数组没有重复。测试代码最终应该打印出 1,2,3,4,5.. 但它实际上打印出 1,2,3,3,4,5 in_array() 函数似乎在这里与变量一起工作?
这是真实的输出:
Avengers
Array thus far: Avengers
Avengers
Array thus far: Avengers
Array thus far: Avengers
American Dad!
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
American Dad!
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Christopher Columbus
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Avatar
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Kung Pao Chicken
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
The Brak Show
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Avengers
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
The Brak Show
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Space Ghost: Coast to Coast
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Array thus far: Space Ghost: Coast to Coast
Battlestar Galactica (2004)
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Array thus far: Space Ghost: Coast to Coast
Array thus far: Battlestar Galactica (2004)
Potstickers
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Array thus far: Space Ghost: Coast to Coast
Array thus far: Battlestar Galactica (2004)
Array thus far: Potstickers
Potstickers
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Array thus far: Space Ghost: Coast to Coast
Array thus far: Battlestar Galactica (2004)
Array thus far: Potstickers
Array thus far: Potstickers
Avatar
Array thus far: Avengers
Array thus far: Avengers
Array thus far: American Dad!
Array thus far: American Dad!
Array thus far: Christopher Columbus
Array thus far: Avatar
Array thus far: Kung Pao Chicken
Array thus far: The Brak Show
Array thus far: Avengers
Array thus far: The Brak Show
Array thus far: Space Ghost: Coast to Coast
Array thus far: Battlestar Galactica (2004)
Array thus far: Potstickers
Array thus far: Potstickers
Array thus far: Avatar
这里有重复的项目,我试图避开阵列。