在 PHP 中,我想构建一个使用 distinct$raw['day']
作为每一行的数组,并计算所有$raw['delay']
大于 0 的数组:
开始
$raw = array(
[0] => ("day" => "2013-01-01", "delay" => 3),
[1] => ("day" => "2013-01-01", "delay" => 16),
[2] => ("day" => "2013-01-01", "delay" => 0),
[3] => ("day" => "2013-01-02", "delay" => 1),
[4] => ("day" => "2013-01-02", "delay" => 0),
[5] => ("day" => "2013-01-03", "delay" => 9),
);
结果
array(
[0] => ("day" => "2013-01-01", "misses" => 2),
[1] => ("day" => "2013-01-02", "misses" => 1),
[2] => ("day" => "2013-01-02", "misses" => 1),
)
我试过的:
我首先在想我会得到所有独特的日子:
$all_days = array_column($raw, 'day');
$unique_days = array_unique($all_days);
现在我已经拥有了所有独特的日子,我可以$raw
再次循环并增加未命中的次数。但这似乎过于冗长,我认为在 PHP 的数组函数中会有一个更简单的解决方案。
foreach ($raw as $row) {
if ($row['delay'] > 0) {
$unique_days[$row['day']]++
}
}