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

时间:2018-03-18 16:54:37

标签: php magento magento-1.9

我需要遍历所有待处理的订单“DONE” 然后遍历每个订单商品并将其保存到DONE数组中 然后调用api,它将返回一个像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;

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

1 个答案:

答案 0 :(得分:0)

据我所知,问题是:

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;
}