$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/