我是PHP的新手。
我有一个mysqli_fetch_array,在while循环中看起来像这样“
array(7) {
["ID"]=>
string(32) "00000000000000000000000000000001"
["NAME"]=>
string(5) "admin"
["PRODUCT"]=>
string(32) "Ps4"
}
array(7) {
["ID"]=>
string(32) "00000000000000000000000000000001"
["NAME"]=>
string(5) "mike"
["PRODUCT"]=>
string(32) "Iphone"
}
array(7) {
["ID"]=>
string(32) "00000000000000000000000000000001"
["NAME"]=>
string(5) "mike"
["PRODUCT"]=>
string(32) "Imac"
}
array(7) {
["ID"]=>
string(32) "00000000000000000000000000000001"
["NAME"]=>
string(5) "mike"
["PRODUCT"]=>
string(32) "LCD Monitor"
}
我想计算admin和mike在循环中出现的次数:到目前为止我知道如何循环:
while($row = mysqli_fetch_assoc($result))
{
//I don't know what to do next here:
}
答案 0 :(得分:2)
试试这个:
echo=FALSE
答案 1 :(得分:2)
您应该从数据库中询问此信息。
像这样的查询可以做
SELECT NAME, count(1) cnt FROM table GROUP BY NAME
这里所有其他答案都因效率低下而尖叫。
答案 2 :(得分:0)
$count = array();
while( $row = mysqli_fetch_assoc($result) ) {
$count[$row['NAME']] = isset( $count[$row['NAME']] ) ? $count[$row['NAME']] + 1 : 1;
}
var_dump($count);