in_array()没有按预期工作

时间:2012-10-04 21:52:35

标签: php arrays

  

可能重复:
  !in_array use in PHP - conditional statments

我的代码正好相反。

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

这里有重复的项目我试图远离数组。

2 个答案:

答案 0 :(得分:2)

您正在保护$output的插入内容,但实际上您正在打印$items,而这些内容根本没有保护。

还要考虑到,根据结果集的预期大小和重复项的可能百分比,允许重复项然后使用array_unique过滤它们可能要快得多(这将需要额外的内存,并且可能被认为是经典的时间/空间权衡。)

答案 1 :(得分: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
        }
    }
}