Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?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);
Copy Clear