<?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);
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- Array
(
[2369868709] => Array
(
[menu_id] => 7
[menu_name] => Kasagbutan Meals
[menu_price] => 100.00
[qty] => 2
[special_instructions] =>
)
[1141584172] => Array
(
[menu_id] => 6
[menu_name] => Coke
[menu_price] => 50.00
[qty] => 1
[special_instructions] =>
)
)
preferences:
144.05 ms | 408 KiB | 5 Q