!在PHP in_array用途 - 条件statments [英] !in_array use in PHP - conditional statments

查看:145
本文介绍了!在PHP in_array用途 - 条件statments的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看我怎么能使用!in_array。

我有这样的code,但它不工作:

 而($行= mysql_fetch_assoc($结果)){        如果(!in_array($行['项目'],$输出)){
            $输出[] = $行;
        }      }   打印(json_en code($输出)); //需要所有行,而不仅仅是['项目']

是的,我knowI正在使用旧的MySQL和code是不是很安全。只是想了解一下检查,如果一个项目是在一个阵列。如果不是,我希望它的商店。如果是,我希望环路跳过...

编辑:
这是一个额外的想法:

  $项目[] =;
而($行= mysql_fetch_assoc($结果)){
    $项目[] = $行['项目'];        如果(!in_array($行['项目'],$项目)){
            $输出[] = $行;
        }
 }
打印(json_en code($输出));

这code将所有的项目在数组中甚至重复;我这样做,以prevent dulicates

我的SQL:

  $结果= mysql_query(
    SELECT b.item,a.rate_id,a.review,c.category,u.username
    从评论的
    INNER JOIN条b
        ON a.item_id = b.items_id
    INNER JOIN master_catÇ
        ON c.cat_id = b.cat_id
    INNER JOIN用户为u
        ON u.user_id = a.user_id
    ORDER BY rate_id DESC LIMIT 15;);


解决方案

 而($行= mysql_fetch_assoc($结果)){    如果(!in_array($行['项目'],$输出)){
        $输出[] = $行['项目'];
    }  }

 而($行= mysql_fetch_assoc($结果)){    如果继续(in_array($行['项目'],$输出)!);
    $输出[] = $行['项目'];  }

I want to see how I can use !in_array.

I have this code, but it doesn't work:

while ($row = mysql_fetch_assoc($result)) {

        if (!in_array($row['item'], $output)) {
            $output[] = $row;
        }

      }

   print(json_encode($output));  // need all rows, not just ['item']

Yes, I knowI am using older mysql and the code is not very "safe". Just want to know about to check if an item is in an array or not. IF it is not, I want it to store. If it is, I want the loop to skip...

EDIT: This is an additional idea:

$items[] = "";
while ($row = mysql_fetch_assoc($result)) {
    $item[] = $row['item'];

        if (!in_array($row['item'], $items)) {
            $output[] = $row;
        }
 }
print(json_encode($output));

This code puts all items in the array even duplicates; I'm doing this to prevent dulicates

My SQL:

    $result = mysql_query("
    SELECT b.item, a.rate_id, a.review, c.category, u.username
    FROM comments a
    INNER JOIN items b
        ON a.item_id = b.items_id
    INNER JOIN master_cat c
        ON c.cat_id = b.cat_id
    INNER JOIN users AS u
        ON u.user_id = a.user_id
    ORDER BY rate_id DESC LIMIT 15;");

解决方案

  while ($row = mysql_fetch_assoc($result)) {

    if (!in_array($row['item'], $output)) {
        $output[] = $row['item'];
    }

  }

or

  while ($row = mysql_fetch_assoc($result)) {

    if (!in_array($row['item'], $output))  continue;
    $output[] = $row['item'];

  }

这篇关于!在PHP in_array用途 - 条件statments的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆