1 回答
TA贡献1900条经验 获得超5个赞
这应该工作。必须更改数组中的一些变量名称,因为您的示例数据不适合代码示例中的变量:
$results = [
[
'iSaleId' => 5027347,
'userId' => 123,
'qty' => 1.0,
'price' => 20.00,
'item' => 'Cheese',
],
[
'iSaleId' => 5027347,
'userId' => 123,
'qty' => 2.0,
'price' => 10.00,
'item' => 'Milk',
],
];
$orderCnt = 0;
$groupOrders = [];
if(count($results) > 0) {
foreach($results as $order){
if (!isset($groupOrders[$order['iSaleId']])) {
$groupOrders[$order['iSaleId']] = [
'id' => $order['iSaleId'],
'userId' => $order['userId'],
'items' => [],
];
$orderCnt++;
}
$groupOrders[$order['iSaleId']]['items'][] = [
'desc' => $order['item'],
'qty' => $order['qty'],
];
}
}
- 1 回答
- 0 关注
- 78 浏览
添加回答
举报