I have php order array and I'm sending this array to shipping service but my clients have 2-3 order with same shop_id
. I'm sending 3th shipping this is problem must be send only 1 shipping with shop_id and must be sum(total) price.
How can I do this? My array output under below.
Array
(
[date] => 2019-05-23
[name] => Alex
[shop_id] => 1
[price] => 13.45
)
Array
(
[date] => 2019-05-23
[name] => Alex
[shop_id] => 1
[price] => 22.45
)
Array
(
[date] => 2019-05-23
[name] => Alan
[shop_id] => 3
[price] => 83.56
)
My array code:
print("<pre>".print_r($orders,true)."</pre>");
My array must be:
Array
(
[date] => 2019-05-23
[name] => Alex
[shop_id] => 1 // LOOK AT SAME shop_id ones in array and
[price] => 35.90 // do total price them and delete 1 more items.
)
Array
(
[date] => 2019-05-23
[name] => Alan
[shop_id] => 3
[price] => 83.56
)
My codes;
$newarray = array();
foreach($arr as $ar)
{
foreach($ar as $k => $v)
{
if(array_key_exists($v, $newarray))
$newarray[$v]['price'] = $newarray[$v]['price'] + $ar['price'];
else if($k == 'shop_id')
$newarray[$v] = $ar;
}
}
print("<pre>".print_r($newarray,true)."</pre>");
Output
Array
(
)