2012-10-04 134 views
-1

可能重复:
!in_array use in PHP - conditional statmentsin_array()无法正常运行

我有这样的代码,做相反。

假设我有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 

有在这里重复的项目,我想保持了阵。

+0

@KickingLettuce:为什么你再次问同样的问题? (感谢jeroen的领导) – Jon

回答

1

你需要按$row['item']$items你做后in_array()

while ($row = mysql_fetch_assoc($result)) { 
    $item = $row['item']; 
    echo $item . '<br />'; // for testing 
    if (!in_array($item, $items)) { 
     $output[] = $row; 
     foreach ($items as $item) { 
      echo 'Array thus far: ' . $item . '<br />'; // for testing 
     } 
     $items[] = $row['item']; 
    } 
} 

或者您也可以通过删除$items简化它:

while ($row = mysql_fetch_assoc($result)) { 
    $item = $row['item']; 
    echo $item . '<br />'; // for testing 
    if (!in_array($item, $output)) { 
     $output[] = $row; 
     foreach ($items as $item) { 
      echo 'Array thus far: ' . $item . '<br />'; // for testing 
     } 
    } 
} 
+0

这工作谢谢!我正在按顺序做事。 – KickingLettuce

2

你守着插入到$output,但你实际打印$items未在所有受保护。

还要考虑到,根据结果集的预期大小和可能的重复百分比,允许重复项然后用array_unique进行筛选可能会快得多(这将需要额外的内存并且可以被认为是经典的时间/空间折衷)。