一个PHP算法,php数组一个二维数组拆分成多个子数组

2020年10月10日17:42:23

一个PHP算法,php数组一个二维数组拆分成多个子数组_第1张图片

真是场景:

一个PHP算法,php数组一个二维数组拆分成多个子数组_第2张图片

条件:
1、名字为张三和李四的num数量不能超过6,超过6就要拆分数组,(数组不一定存在张三李四)
2、数组所有人的num加起来不能超过30,超过30也要拆分成小数组。(小数组个数不限,满足以上2个条件即可)

3、还有一个条件哦,就是张三和李四如果在同一个子数组里面,他们的数量加起来 和也不能大于6。。

代码:



function _csz($arr)
{
    $coder = ['6971632090049','6922994301397']; // 大件货物的条码
    $date = func_get_args();
    $num = (int)$date[2];
    if (in_array($date[1], $coder)) {
        $jishu = 6; // 大件货物爆满个数
        $yushu = $num;
        $beishu = 0;
        if ($num > $jishu) {
            $yushu = $num % $jishu;
            $beishu = floor($num / $jishu);
        }

    } else {
        $jishu = 30;    // 小件货物爆满个数
        $yushu = $num;
        $beishu = 0;
        if ($num > $jishu) {
            $yushu = $num % $jishu;
            $beishu = floor($num / $jishu);
        }
    }

    if ($beishu > 0) {
        for ($i = 0; $i < $beishu; $i++) {
            $ret[$i] = ['name' => $date[0], 'bar_code' => $date[1], 'quantity' => $jishu, 'scan_mode' => $date[3]];
        }
    }
    if ($yushu > 0) {
        $ret['yushu'] = ['name' => $date[0], 'bar_code' => $date[1], 'quantity' => $yushu, 'scan_mode' => $date[3]];
    }
    return $ret;
}

function _hsz($arr, $base_num, &$return,$index)
{
    $count = count($arr);
    for ($i=0;$i<$count;$i++){
        if(!empty($return)){
            $num = 0;
            foreach ($return[$index] as $key=>$val){
                $num += $val['quantity'];
            }
            if(($num + $arr[$i]['quantity']) > $base_num){
                ++$index;
                $return[$index][] = $arr[$i];
                unset($arr[$i]);
                if( count($arr) > 0){
                    $arr = array_values($arr);
                    return _hsz($arr, $base_num, $return,$index);
                }
                return $return;
            }else{
                $return[$index][] = $arr[$i];
                unset($arr[$i]);
                $arr = array_values($arr);
                if(sizeof($arr) > 0){
                    return _hsz($arr, $base_num, $return,$index);
                }
            }
        }else{
            $return[$index][] = $arr[$i];
            unset($arr[$i]);
            //$arr = array_values($arr);
        }
    }
    return $return;
}

/**
 * 自动拣货器,拆分包裹-算法
 * 2020-9-22 18:04:59
 * @param $item
 * @return array
 */
function getGoodsArray($item)
{


    $site6 = $site30 = $info = [] ;
    foreach ($item as $k => $v){
        $arr = call_user_func_array('_csz', $v);
        if(isset($arr['yushu'])){
            if(in_array($arr['yushu']['bar_code'], ['6971632090049','6922994301397'])){
                $site6[] = $arr['yushu'];
            }else{
                $site30[] = $arr['yushu'];
            }
        }
        unset($arr['yushu']);
        if(!empty($arr)){
            $info[] = $arr;
        }
    }
    $return6 = $return30 =[];
    $index1 = $index2 = 0;
    if(!empty($site6)){
        _hsz($site6, 6, $return6, $index1);
    }

    if(!empty($site30)){
        _hsz($site30, 30, $return30, $index2);
    }

    $info_son = [];
    if(is_array($info)){
        foreach ($info as $in){
            $info_son = array_merge($info_son,$in);
        }
    }

    $rrt = array_merge($info_son,$return6,$return30);

    return $rrt;
    //var_dump($rrt);
    //var_dump($rrt);
    //var_dump($return6,$return30);
}


模拟数组:

$goods =  [
            20200702024 =>  [
            "name" => "LP-胶囊雨伞",
            "bar_code" => "20200702024",
            "quantity" => "1",
            "scan_mode" => "5",
          ],
          6970328080043 =>  [
                "name" => "LP-果冻压缩面膜",
                "bar_code" => "6970328080043",
                "quantity" => "1",
                "scan_mode" => "5",
          ],
          6922994301816 =>  [
                "name" => "LP-兰希黎纯棉洁面巾",
                "bar_code" => "6922994301816",
                "quantity" => "1",
                "scan_mode" => "3",
          ],
            6922994300765 =>  [
                "name" => "兰希黎海洋鲜藻微精华水漾面膜",
                "bar_code" => "6922994300765",
                "quantity" => "5",
                "scan_mode" => "4",
            ],
            6922994300192 =>  [
                "name" => "兰希黎洋甘菊舒缓纯露",
                "bar_code" => "6922994300192",
                "quantity" => "11",
                "scan_mode" => "1",
            ],
        ];

        $rt = getGoodsArray($goods);
        var_dump($rt);
        exit;

还有个问题:

这个sql可以搜索到东西,frame_code是varchar(64)
select * from order_frame_log where frame_code = 18000013

这个sql搜不到,frame_code是varchar(64),为什么呢
select * from order_frame_log where frame_code = '18000013'

 

你可能感兴趣的:(PHP开发,php,算法,数据结构,mysql)