我有一個像這樣的陣列
Array ( [0] => Array ( [0] => 2023-06-28 [1] => 5482 ) [1] => Array ( [0] => 2023-06-28 [1] => 316 ) [2] => Array ( [0] => 2023-06-28 [1] => 189 ) [3] => Array ( [0] => 2023-06-29 [1] => 5 ) [4] => Array ( [0] => 2023-06-29 [1] => 0 ) [5] => Array ( [0] => 2023-06-30 [1] => 5788 ) [6] => Array ( [0] => 2023-06-30 [1] => 1266 ) )
我想按日期分組並求和值
$output=array(); foreach($array as $values){ $date = $values[0]; $output[$d][1]+=$values[1]; }
$output的結果是
Array ( [0] => Array ( [0] => 2023-06-28 [1] => 5987 ) [1] => Array ( [0] => 2023-06-29 [1] => 5 ) [2] => Array ( [0] => 2023-06-30 [1] => 7054 ) )
這一切都沒問題,但是我需要計算平均值,而不是求和值,所以我的想法是在foreach中獲取同一天的總和,然後將總和除以同一天的數量。例如,對於日期2023-06-28,總和是5987,數量是3,所以結果應該是5987 / 3。
有沒有辦法可以做到這一點,或者在foreach中獲得平均值的其他方法?
雷雷