JoeHandsome 2020-07-10 11:30:44 242次浏览 0条评论 0 0 0

封装的方法如下:

     /**
     * 用求余数的方法进行切割二维数组
     * @param array $arr  二维数组 [['12020202002'],['12121212210250250']]
     * @param int $num 每个数组切割的数量
     * @return array
     */
    public static function arr_slice($arr=[],$num=5){
        $temp = [];
        $last = [];
        $count = 0;
        foreach ($arr as $k=>$v){
            $count++;
            if ($count%($num+1) != 0){
                array_push($temp,$v[0]);
            }else{
                array_push($last,$temp);
                $temp = [];
                array_push($temp,$v[0]);
            }
            if ($count == count($arr)){
                array_push($last,$temp);
            }
        }
        return $last;
    }

调用结果:

    public function actionTest(){
        $arr = [
            ['10200102022010'],
            ['10200102022011'],
            ['10200102022012'],
            ['10200102022013'],
            ['10200102022014'],
            ['10200102022015'],
            ['10200102022016'],
            ['10200102022017'],
            ['10200102022018'],
            ['10200102022019'],
            ['10200102022020'],
            ['10200102022021'],
            ['10200102022022'],
            ['10200102022023']
        ];

        $last = self::arr_slice($arr,5);
        var_dump($last);die;
    }

如图:

微信截图_20200710113804.png

亿速云
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册