2012-10-04 132 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進行篩選可能會快得多(這將需要額外的內存並且可以被認爲是經典的時間/空間折衷)。