php递归函数中清空静态变量引起个bug

代码块:

/**
 * 第二版分框算法 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;

 修复之后,返回正常如下:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值