Could you please give me suggestion to solve this problem?
i got a datas like:
orders =[
{
orderID:1,
Name: 'Sam',
orderList: [
{SKU: 'A001',Qty: 2},{SKU: 'A003',Qty: 4}
]
},
{
orderID:2,
Name: 'Sam',
orderList: [
{SKU: 'A001',Qty: 1},{SKU: 'A002',Qty: 1}
]
},
{
orderID:3,
Name: 'Sam',
orderList: [
{SKU: 'A001',Qty: 2},{SKU: 'A004',Qty: 2}
]
},
{
orderID:4,
Name: 'Sam',
orderList: [
{SKU: 'A003',Qty: 3},{SKU: 'A002',Qty: 1}
]
},
{
orderID:5,
Name: 'Sam',
orderList: [
{SKU: 'A002',Qty: 1},{SKU: 'A003',Qty: 1}
]
}
]
//after foreach i want to output each order items and gather their SKU and qty like:
foreach ($orders as $order) {
echo <td>$order.orderList.sku</td>
echo <td>$order.orderList.qty</td>
//output a table like:
/*
item | Qty
A001 | 5
A002 | 4
A003 | 8
A004 | 2
B001 | 1
/*
}
Please give me some suggestion, thank you so much!
2
Answers
You can achieve your requirement by:
SKU
and their total quantities.SKU
.demo here
Ref: array_key_exists
To achieve the desired output, you need to iterate through each order, then iterate through each item in the order’s
orderList
array to gather SKU and quantity information. You can then aggregate this information into an associative array where the keys are SKUs and the values are the sum of quantities. So here is my suggested code:This script will output a table with each SKU and its corresponding quantity, aggregated from all the orders in the
$orders
array. Hope this helps.