两个for循环合并成一个数组放入txt文件中

发布时间:2020-03-21作者:小灵龙点击:177

脚本

           public function enable_code_create(){
        $post = input('post.');
        if(empty($post))
        {
          echo returnCodeWithOutData('1002','参数不能为空');exit;
        }
        if(empty($post['coupon_id'])){

          echo returnCodeWithOutData('1002','优惠券ID不能为空');exit;
        }
        $couponamount=count($post['coupon_id']);//选中了几个优惠券ID,可能选中一个或者选中两个
        $gifta=$post['gift_id'];

        $batch = Db::name('yxk_card')->where('gift_id', $gifta)->order('batch_id','desc')->find();
            if(empty($batch))
            {
                $batch_id = 1;
            }else{
                $batch_id = $batch['batch_id']+1;
            }
            $num=0;
        for($s=0; $s < $couponamount; $s++) {

            $data = array();
            $count = $post['count'][$s];//数量
            $digit =16;//生成位数,位数要是4的倍数
            for($i=0;$i<$count;$i++){
                $code = $this->encodeID($digit);
                $data['code'] = md5('110'.$code);
                $data['gift_id'] =$gifta;//礼包ID
                $data['coupon_id'] = $post['coupon_id'][$s];//优惠券ID
                $data['create_time'] = time();
                $data['lose_time'] = strtotime($post['lose_time'][$s]);//失效时间
                $data['batch_id'] = $batch_id;//礼包批次
                $result[$i] = Db::name('yxk_card')->insertGetId($data);
                $arr[$num]['id'] = $result[$i];
                $arr[$num]['code'] = $code;
                unset($data);
                $num++;
            }
        }
        //创建类型
        $path = './ud/ie/yde/';
        if (!file_exists($path)) {
            mkdir($path,0777,true);
        }
        file_put_contents($path.$gifta.'-'.$batch_id.'.txt', json_encode($arr));

标签: