<?php
$arr = [
[
"menu_id" => 7,
"menu_name" => "Kasagbutan Meals",
"menu_price" => "100.00",
"qty" => "1",
"special_instructions" => ""
],
[
"menu_id" => 7,
"special_instructions" => "",
"menu_name" => "Kasagbutan Meals",
"menu_price" => "100.00",
"qty" => "1",
],
[
"menu_id" => 6,
"menu_name" => "Coke",
"menu_price" => "50.00",
"qty" => "1",
"special_instructions" => ""
]
];
$exclude = array_flip(['qty']);
$totals = array_reduce($arr, function($totals, $item) use ($exclude) {
$id = array_diff_key($item, $exclude);
ksort($id);
$hash = crc32(json_encode($id));
if (array_key_exists($hash, $totals)) {
$totals[$hash]['qty'] += $item['qty'];
} else {
$totals[$hash] = $item;
}
return $totals;
}, []);
print_r($totals);
preferences:
24.76 ms | 410 KiB | 5 Q