如何计数重复的值内,而循环使用php


how to count duplicates values inside while loop using php

$query =" select * from tableitem";
$result = 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循环中。

第1项2项
第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);

如果您使用要计数的键来增加数组值:

$check = array();
foreach($values as $key => $value) {
   if (isset($check[$key])) {
       $check[$key]++;
   } else {
       $check[$key]=1;
   }
}
var_dump($check);