如何对嵌套嵌套数组进行操作

How to do operations on nested nested arrays

我需要遍历所有挂单"DONE" 然后遍历每个订单项并将它们保存到数组 DONE 然后调用 api 它将 return 像这样的响应 DONE

{"response": 
[{"Lookup":"SKU","Quantity":3,"StoreName":"Shop1"}, 
{"Lookup":"SKU","Quantity":30,"StoreName":"Shop2"}, 
{"Lookup":"SKU","Quantity":15,"StoreName":"Shop3"}, 
{"Lookup":"SKU","Quantity":50,"StoreName":"Shop4"}]}

之后我需要先查看Shop3的数量,如果大于订购的数量,则从那里取走并保存新的数量,如果还有剩余数量,则循环剩余的数量商店,然后从数量最多的商店拿走,这就是我被困的地方。如果我遍历了所有的商店,还有数量需要订购,我也应该报告。

到目前为止我所做的就是这些。

$requiredItems--> 是一个准确的数组(sku => 订购数量)

  $report = array();
  $allItemsStoreQty = array();
  $unavailableItems = array();

  foreach ($requiredItems as $sku => $qty) {
    $remainingQty = $qty;
    $itemStoreQty = array();

    //Get Stock Breakdown
    $stock = $this->getStock($sku);

    if ($stock['Shop3'] >= $qty) {
        $storeQty = array();
        $storeQty['Shop3'] = $qty;
        array_push($itemStoreQty, $storeQty);

    } else {


        if ($stock['Shop3'] < $qty) {
            $storeQty = array();
            $storeQty['Shop3'] = $qty;
            $remainingQty = $qty - $stock['Shop3'];
            $stock['Shop3'] = 0;

            array_push($itemStoreQty, $storeQty);
        }

        arsort($stock);

        foreach ($stock as $storeStockName => $storeStockQty) {
            if ($remainingQty > 0 && $storeStockQty > 0) {
                $currentQty = $remainingQty - $storeStockQty;
                if ($currentQty <= 0) {
                    $storeQty = array();
                    $storeQty[$storeStockName] = $remainingQty;
                    array_push($itemStoreQty, $storeQty);

                    $remainingQty = 0;
                } else {
                    $storeQty = array();
                    $storeQty[$storeStockName] = $currentQty;
                    array_push($itemStoreQty, $storeQty);

                    $remainingQty = $currentQty;
                }
            }
        }

        $allItemsStoreQty[$sku] = $itemStoreQty;

    }

    if ($remainingQty > 0) {
        $unavailableItems[$sku] = $remainingQty;
    }

}

$report['available'] = $allItemsStoreQty;
$report['unavailable'] = $unavailableItems;

return $report;

但是报告的数字有误,我不知道我的错误在哪里,我知道它在逻辑上的某个地方:(但我找不到它。

据我所知,问题是:

if ($stock['Shop3'] >= $qty) {
    $storeQty = array();
    $storeQty['Shop3'] = $stock['shop3']; // not $qty
    array_push($itemStoreQty, $storeQty);

}

} else {
   $storeQty = array();
   $storeQty[$storeStockName] = $storeStockQty; // not $currentQty;
   array_push($itemStoreQty, $storeQty);

   $remainingQty = $currentQty;
}