Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
$arr = [
["order_no" => 222, "month" => "Aug-22", "totalAmount" => 2305],
["order_no" => 333, "month" => "Aug-22", "totalAmount" => 945],
["order_no" => 1, "month" => "Sep-22", "totalAmount" => 945],
["order_no" => 111, "month" => "Sep-22", "totalAmount" => 2305],
];
$res = array_reduce(
$arr,
function($acc, $order) {
if (isset($acc[$order['month']])) {
$acc[$order['month']]['totalAmount'] += $order['totalAmount'];
} else {
$acc[$order['month']] = [
"month" => $order['month'], "totalAmount" => $order['totalAmount']
];
}
return $acc;
},
[]
);
print_r($res);