我有一些问题,我对这个代码进行分组并优先减少商店进行销售,我想做的是查看每个商店但首先查看优先级store_id,如果没有继续寻找其他商店,直到我达到要求的数量,如果在1个以上的商店分配数量,我需要分割产品并获得数量,直到我达到所有要求的数量。
包含所请求产品的数据和库存信息
$data = array(
array(
'product_id' => 65676,
'quantity' => 10,
'stock' => array(
array(
'product_id' => 65676,
'store_id' => 1,
'qty' => 3,
),
array(
'product_id' => 65676,
'store_id' => 2,
'qty' => 3
),
array(
'product_id' => 65676,
'store_id' => 3,
'qty' => 8
)
)
),
array(
'product_id' => 34742,
'quantity' => 3,
'stock' => array(
array(
'product_id' => 34742,
'store_id' => 3,
'qty' => 5
),
array(
'product_id' => 34742,
'store_id' => 2,
'qty' => 5
)
)
),
);
提取优先级商店ID的功能
function searchForIdDefaultStore($store_id, $array) {
foreach ($array as $key => $val) {
if ($val['store_id'] === $store_id) {
return $key;
}
}
return null;
}
按产品分组的作业
echo '<pre>';
/* I need to have a prioritar store */
/* I need to group by product_id and store_id so if all items are in the same store i need to send to that store */
$storePrioritar = 1;
foreach($data as $dat){
$arrayItems[] = $dat['product_id']; //May i check with this if all items i passe trough exist in new array.
$getKey = $this->searchForIdDefaultStore($storePrioritar, $dat['stock']); //Get prioritar store_id
if(!is_null($getKey)){
//If the quantity in store_id is bigger or equal than requested.
if($dat['stock'][$getKey]['qty'] >= $dat['quantity']){
$grouped[$dat['product_id']][] = array(
'product_id' => $dat['product_id'],
'store_id' => $dat['stock'][$getKey]['store_id'],
'neeQty' => $dat['quantity'],
'haveQty' => $dat['stock'][$getKey]['qty']
);
}else{
//If not i will loop over all stores and get there where i foudn the item.
foreach($dat['stock'] as $stock){
if($stock['product_id'] == $dat['product_id']){
//If the quantity is bigger or equal than requested
if($stock['qty'] >= $dat['quantity']){
$grouped[$stock['product_id']][] = array(
'product_id' => $dat['product_id'],
'store_id' => $stock['store_id'],
'neeQty' => $dat['quantity'],
'haveQty' => $stock['qty']
);
}else{
//If not just give me what is available
$grouped[$stock['product_id']][] = array(
'product_id' => $dat['product_id'],
'store_id' => $stock['store_id'],
'neeQty' => $dat['quantity'],
'haveQty' => $stock['qty']
);
}
}
}
}
}else{ //If i have no prioritar i will group by product_id
foreach($dat['stock'] as $stock){
if($stock['product_id'] == $dat['product_id']){
//If the quantity is bigger or equal than requested
if($stock['qty'] >= $dat['quantity']){
$grouped[$stock['product_id']][] = array(
'product_id' => $dat['product_id'],
'store_id' => $stock['store_id'],
'neeQty' => $dat['quantity'],
'haveQty' => $stock['qty']
);
}else{
//If not just give me what is available
$grouped[$stock['product_id']][] = array(
'product_id' => $dat['product_id'],
'store_id' => $stock['store_id'],
'neeQty' => $dat['quantity'],
'haveQty' => $stock['qty']
);
}
}
}
}
}
print_r($grouped);
/* The problem now is that i have 2 Products ID with stock and i need to group and substract the quantity if the requested quantity is not enough in that store */
My question is, how can i group and sum/rest divide for each store in one loop ?
我期望的结果是:
array(
1 => array(
array(
'product_id' => 65676,
'store_id' => 1,
'quantity' => 2,
)
),
3 => array(
array(
'product_id' => 65676,
'store_id' => 3,
'quantity' => 8,
),
array(
'product_id' => 34742,
'store_id' => 3,
'quantity' => 3,
)
)
)
Note: the key is the store_id