php - 如何使用php计算while循环内的重复值

标签 php count

$query = "从表项中选择 *";
$结果 = mysql_query($query);

$col = array();

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

 //they have computation here inside loop.
  $amount = $value;

if($row['status']>3){  // base on the status only
    if($amount>0){ //base on the computation 

        $count = $item;
        // i need to count here the item before the grouping of duplicate item below.

    if (!isset($col[$row['item']])) { // this is working for grouping the duplicate value
       echo $row['item'];  
       echo $count;   // count item 
       $col[$row['item']] = true;
      }
   }
 }}

如果可能的话,示例输出在 while 循环内应该像这样。

项目一2
第二项3
项目五4

最佳答案

你可以用 sql 来做到这一点,不需要 php;

SELECT COUNT(*) as N,item 
FROM tableitem
GROUP BY item

它将返回重复的项目,您可以使用n>1进行检查。您还可以为组添加更多列。

$itemArray;
while ($row = mysql_fetch_array($result)){
    if($row['n']>1){
       itemArray[] = $row['item'];    
    }    
}
print_r($itemArray);

关于php - 如何使用php计算while循环内的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30410883/

相关文章:

javascript - JQuery 阻止链接在加载 ajax 时工作

php - 通过 php 插件命令显示背景图片

java - 如何让这个程序计算用户输入单词中的元音?

java - Spring 数据JPA : findAll with Specification and Pageable fails on count query

MySQL - COUNT、GROUP BY、ORDER BY 并获取最新日期时间

php - 尝试使用 PHP 和 html 显示来自 mysql 的数据

php - 内部连接性能慢

php - Angular 和 Laravel 之间的路由

mysql - 在使用 COUNT() 和 GROUP BY 时如何包含 NULL 计数,因为 COUNT 会忽略 MySQL 中的 NULL?

LINQ 分组和最大计数