3 回答

TA貢獻1811條經驗 獲得超6個贊
您只需要測試您感興趣的值,然后將其添加到累加器中(如果它是正確的值)$row['member_group_id']
$countLeaders = 0;
while($row = mysqli_fetch_assoc($result)){
if ($row['member_group_id'] == '4') {
$countLeaders++;
}
// other code if you have it
}
echo "<span>" . $countLeaders . " Leaders</span>";

TA貢獻1830條經驗 獲得超9個贊
如果我理解,你從你的查詢中得到的所有結果都是一個數組,類似于:pg_fetch_all
$result = [
[
'id' => 'whatever',
'and' => 'whatever',
'other' => 'whatever'
'fields' => 'whatever'
'member_group_id' => 1,
],
[
'id' => 'whatever',
'and' => 'whatever',
'other' => 'whatever'
'fields' => 'whatever'
'member_group_id' => 3,
],
[
'id' => 'whatever',
'and' => 'whatever',
'other' => 'whatever'
'fields' => 'whatever'
'member_group_id' => 1,
],
]
您想知道有多少條記錄['member_group_id'] == 1
您可以使用
count(
array_filter(
function($item) { return ($item['member_group_id'] == 1); },
$result
)
)
array_filter遍歷數組并僅保留函數返回 true 的項。

TA貢獻1784條經驗 獲得超2個贊
你不能在同一級別有多個時間相同鍵的數組,我解釋說:
$test['hello'] = 1;
$test['hello'] = 5;
因此,鍵將僅包含 。如果要計算具有相同值的所有鍵,可以編寫如下代碼:hello5
$array = array('1', '1', '2', 'test' => '1', '1', '2', '3', '1', '9');
$count = \count(array_keys($array, '1'));
// output of $count is 5
搜索與指定值對應的所有鍵,然后對所有鍵進行計數。
- 3 回答
- 0 關注
- 147 瀏覽
添加回答
舉報