如您所见,我有一个具有重复订单编号的数组 因为一个订单可以有多个库存商品(stockitemname)。如何合并数组,以便汇总总字段,不再重复订购编号,并将所有库存商品名称置于相同的orderId下?
有人能解决这个问题吗?
Array ( [0] => Array
([orderId] => 1544100294
[total] => 215.28
[receivedate] => 0000-00-00
[stockitemname] => "The Gu" red shirt XML tag t-shirt (White) XXS
[customerid] => 10 )
[1] => Array
( [orderId] => 1544119651
[total] => 37.38
[receivedate] => 0000-00-00
[stockitemname] => USB rocket launcher (Gray)
[customerid] => 10 )
[2] => Array
( [orderId] => 1544100294
[total] => 1614.60
[receivedate] => 0000-00-00
[stockitemname] => Large sized bubblewrap roll 50m
[customerid] => 10 ) )
根据查询结果创建数组:
SELECT oc.orderId, SUM(so.quantity*si.RecommendedRetailPrice) total, oc.receivedate, si.stockitemname, ru.customerid
FROM orderbycustomers oc
JOIN registered_users ru
ON oc.customerid = ru.customerid
JOIN stockitemorders so
ON oc.orderId = so.orderId
JOIN stockitems si
ON so.StockItemID = si.StockItemID
WHERE oc.customerid = $customerid
GROUP BY si.stockitemname
ORDER BY oc.receivedate
答案 0 :(得分:1)
一种以 顺序 收集stockitems
的方法可能是这样的:
<?php
$result = [];
foreach ($ordersFromQuery as $order) {
$result[$order['orderId']][] = $order;
}
?>
答案 1 :(得分:1)