I'm trying to add sum all values of a specific key to an array devided into months, but can't figure out how to do it. I manage to get the first value, but trying to add on to that only gives me errors.
$accumulatedMonthly = DB::table('sold_tickets')
->select('price', 'created_at')
->where('event_id', $id)
->where('credited', null)
->where('user_id', '!=', null)
->orderBy('created_at')
->get()
->groupBy(function($val) {
return Carbon::parse($val->created_at)->format('M y');
});
$accumulatedMonthly = json_decode(json_encode($accumulatedMonthly), true);
$accumulatedPerMonth = [];
foreach ($accumulatedMonthly as $k => $month) {
foreach ($month as $m) {
$accumulatedPerMonth[$k] = $m['price'];
}
}
I would like the result to be devided into months with all the 'price'-values added on top of eachother. Now I get the months correctly, but only the first value of each month.
This is the output currently
Array
(
[Aug 16] => 999
[Nov 16] => 1399
[Dec 16] => 1399
[Jan 17] => 1399
[Feb 17] => 1599
[Mar 17] => 1599
[Apr 17] => 1599
[May 17] => 1599
[Jun 17] => 1599
[Jul 17] => 1599
[Aug 17] => 1199
)