我有这样的命令
product quantity inventory
1 5 50
1 6 50
7 2 150
1 6 50我试图在每种产品中循环并减少库存。
产品1的总库存为50
in循环
product 1 the inventory become 45
product 1 the inventory becomes 39
product 7 the inventory becomes 148
product 1 the inventory becomes 44她的问题是在最后一个循环中,库存又到了50。
这是我的密码
foreach($order->productId->inventory as $currentRequestedCount){
$currentRequested = $order->quantity * $order->relatedPackage ->unit_count;
if($currentRequestedCount->type == "existing"){
$currentRequested -= $currentRequestedCount->amount;
}
}如何防止$currentRequestedCount被重置?
发布于 2020-11-14 16:08:43
我使用数组来说明这一点。你有你的存货:
$inventory = [
//product => inventory
1 => 50,
7 => 150,
];你接到几个命令:
$order = [
['product' => 1, 'quantity' => 5],
['product' => 1, 'quantity' => 6],
['product' => 7, 'quantity' => 2],
['product' => 1, 'quantity' => 6],
];订单被处理(没有处理错误!)。
foreach($order as $row){
$inventory[$row['product']] -= $row['quantity'];
}目前的清单:
var_dump($inventory);输出:
array(2) {
[1]=>
int(33)
[7]=>
int(148)
}https://stackoverflow.com/questions/64834890
复制相似问题