代码之家  ›  专栏  ›  技术社区  ›  Shalto

如何对嵌套数组执行操作

  •  0
  • Shalto  · 技术社区  · 6 年前

    我需要循环查看所有待处理的订单“完成” 然后循环遍历每个订单项,并将它们保存到一个已完成的数组中 然后调用api,它将返回这样的响应完成

    {"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中的数量,如果数量大于订购数量,则从那里取下并保存新数量,如果仍有剩余数量,则循环通过其他商店,从数量最多的商店取下,这是我遇到的问题。如果我浏览了所有商店,但仍有数量需要订购,我也应该报告。

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

    $需求项-->是精确的(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 回复  |  直到 6 年前
        1
  •  0
  •   A Loewer    6 年前

    据我所知,问题是:

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