skip to Main Content

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


  1. You can achieve your requirement by:

    • Creating an associative array to store the SKU and their total quantities.
    • Iterate through the orders and add the quantities for each SKU.
    • Print the output in a table format.
    $skuTotals = [];
    
    foreach ($orders as $order) {
        foreach ($order['orderList'] as $item) {
            if (array_key_exists($item['SKU'], $skuTotals)) {
                $skuTotals[$item['SKU']] += $item['Qty'];
            } else {
                $skuTotals[$item['SKU']] = $item['Qty'];
            }
        }
    }
    
    # Print table elements or perform actions as per your requirement
    foreach ($skuTotals as $sku => $qty) {
        echo "<tr><td>$sku</td><td>$qty</td></tr>";
    }
    

    demo here


    Ref: array_key_exists

    Login or Signup to reply.
  2. 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:

    <?php
    
    $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]
            ]
        ]
    ];
    
    // Initialize an associative array to store SKU and quantity information
    $skuQuantities = [];
    
    // Iterate through each order
    foreach ($orders as $order) {
        // Iterate through each item in the order's orderList array
        foreach ($order['orderList'] as $item) {
            $sku = $item['SKU'];
            $qty = $item['Qty'];
            
            // If SKU already exists in $skuQuantities, add quantity to existing quantity, otherwise set quantity
            if (isset($skuQuantities[$sku])) {
                $skuQuantities[$sku] += $qty;
            } else {
                $skuQuantities[$sku] = $qty;
            }
        }
    }
    
    // Output the table
    echo "<table>";
    echo "<tr><th>Item</th><th>Qty</th></tr>";
    foreach ($skuQuantities as $sku => $qty) {
        echo "<tr><td>$sku</td><td>$qty</td></tr>";
    }
    echo "</table>";
    ?>
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search