3 回答

TA贡献1802条经验 获得超10个赞
您可以在循环内添加计数:
while($getwpi = $getwpicon->fetch_assoc()){
$year = date('Y', strtotime($getwpi['datum']));
$getwpi['datum'] = $year; // update your field
if ($getwpi['answer'] == "fout") {
$res[$year] = isset($res[$year]) ? $res[$year] + 1 : 1;
}
$wpi[] = $getwpi; // add to the result array
}
现在,$res将使用每年计数的数组。您可以在其上循环打印所需的内容。

TA贡献1797条经验 获得超6个赞
您可以使用array_walk和array_key_exists来解决这个问题
$res=[];
array_walk($arr, function($v, $k) use (&$res){//$arr is the main array
$res[$v['datum']] = (array_key_exists($v['datum'],$res) && !empty($v['answer'])) ? ($res[$v['datum']]+=1) : 1;
});
echo '<pre>';
print_r($res);
输出示例:
Array
(
[2019] => 4
)
- 3 回答
- 0 关注
- 168 浏览
添加回答
举报