代码块:
/**
* 第二版分框算法 2021-11-17 17:06:18
* @param $orders
* @param $i $i 第一次为空,会清空静态变量,下次调用时已经++ ,故不会在清空
* @return array
*/
function split_order(&$orders, $i = 0)
{
$bar_code = ['6971632090049','6922994301397','6971632090032','6922994301984','6922994301700'];
$big = env('BIG_LOGISTIC',6);
$small = env('TOTAL_LOGISTIC',30);
if(empty($orders)) return [];
static $item = [];
if(empty($i))
{
$item = [];
}
foreach ($orders as $k => &$v){
if(in_array($v['bar_code'], $bar_code)){
if($v['quantity'] > $big){
$temp_big = $temp_many = $v;
$temp_big['quantity'] = $big;
$temp_many['quantity'] = $v['quantity'] - $big;
$item[][] = $temp_big;
$v['quantity'] -= $big;
array_push($orders, $temp_many);
unset($orders[$k]);
//split_order($orders);
}
}else{
if($v['quantity'] > $small){
$temp_small = $tem_many_small = $v;
$temp_small['quantity'] = $small;
$tem_many_small['quantity'] = $v['quantity'] - $small;
//var_dump($item);
$item[][] = $temp_small;
$v['quantity'] -= $small;
array_push($orders, $tem_many_small);
unset($orders[$k]);
//echo '--================------'. $v['quantity'] .'<hr>';
$i ++;
split_order($orders, $i);
}
}
}
//return $item;
$rt_arr = $item;
//$item = []; // 在这里置空不行,第一次递归的数据丢失,bug 2021-11-18 09:57:46
return [$rt_arr, $orders];
}
测试:
$goods4 = [
6922994301397 => [
"name" => "兰希黎鎏光皙透多肽焕颜紧致原液",
"bar_code" => "6922994301397",
"quantity" => "18",
"scan_mode" => "4",
],
6973030920034 => [
"name" => "朵优谷玫桂营养素饮品",
"bar_code" => "6973030920034",
"quantity" => "31",
"scan_mode" => "5",
]
];
//$rt = getGoodsArray($goods);
$rt = split_order($goods4, []);
//dd($rt);exit;
$arr2 = split_order_thirty($rt[1]);
//var_dump($arr2);exit;
if(empty($rt[0])){
$new_arr = $arr2;
}else{
$new_arr = array_merge($rt[0], $arr2);
}
var_dump($new_arr);exit;
修复之后,返回正常如下: