1.自己写的做个记录
try { $str = '[{"pageid":"270"},{"que":[{"title":"sad","type":"1","option_a":"sad","option_b":"sad","option_c":"dad","option_d":"ad","option_e":"d","option_f":"da","option_g":"sda","option_h":"d","answer":"sdad"},{"title":"sdaf","type":"1","option_a":"sfa","option_b":"fa","option_c":"da","option_d":"d","option_e":"d","option_f":"dd","option_g":"ad","option_h":"add","answer":"ad"},{"title":"wedwq","type":"1","option_a":"ea","option_b":"aaas","option_c":"ssssdas","option_d":"ad","option_e":"ad","option_f":"adad","option_g":"wdqd","option_h":"ws","answer":"ada"},{"title":"dwq","type":"1","option_a":"ad","option_b":"ad","option_c":"dad","option_d":"dad","option_e":"ad","option_f":"dw","option_g":"ad","option_h":"sad","answer":"d"},{"title":"af","type":"1","option_a":"sad","option_b":"a","option_c":"af","option_d":"saf","option_e":"asfdf","option_f":"da","option_g":"fs","option_h":"dasf","answer":"afa"}]}]'; $info = json_decode($str); $arr = $info[1]; $value=''; foreach ($arr as $k=>$value) { $value=$value; } $pageid=$info[0]->pageid; foreach ($value as $info){ $arr1 = [ 'title' => $info->title,//题目 'type' => $info->type,//题目类型 'option_a' => $info->option_a,//A 'option_b' => $info->option_b,//B 'option_c' => $info->option_c,//C 'option_d' => $info->option_d,//D 'option_e' => $info->option_e,//E 'option_f' => $info->option_f,//F 'option_g' => $info->option_g,//G 'option_h' => $info->option_h,//H 'answer' => $info->answer,//正确答案 'pageid'=>$pageid, ]; DB::table('v')->insert($arr1); } } catch (Exception $exception) { $exception->getMessage(); }
数据库信息
CREATE TABLE `v` ( `id` int(4) NOT NULL AUTO_INCREMENT, `title` varchar(200) NOT NULL, `type` int(1) DEFAULT NULL, `option_a` varchar(100) DEFAULT NULL, `option_b` varchar(100) DEFAULT NULL, `option_c` varchar(100) DEFAULT NULL, `option_d` varchar(100) DEFAULT NULL, `option_e` varchar(100) DEFAULT NULL, `option_f` varchar(100) DEFAULT NULL, `option_g` varchar(100) DEFAULT NULL, `option_h` varchar(100) DEFAULT NULL, `answer` varchar(100) DEFAULT NULL, `score` int(11) DEFAULT NULL, `pageid` int(11) DEFAULT NULL, `token` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), KEY `token` (`token`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=105 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='知识竞赛表';
3.自己写的版本二
1 public function store(Request $request) 2 { 3 $str=$request->except('_token'); 4 $str=implode($str);//将数组转化为字符串(由于传输过来的是单个数组所以只能转化为字符串进行操作) 5 $info = json_decode($str);//转换为json数组 6 $arr = $info[1];//得到数组第一小数组个信息 7 $value='';//下面循环将第二个数组的首字符去掉然后保存为新的数组 8 foreach ($arr as $k=>$value) { 9 $value=$value; 10 } 11 $pageid=$info[0]->pageid;//得到id 12 $type_num=PolicePage::find($pageid); 13 $type_num=$type_num->page_type; 14 switch ($type_num){ 15 case 0: 16 foreach ($value as $info){ 17 $arr1 = [ 18 'title' => $info->title,//题目 19 'type' => $info->type,//题目类型 20 'option_a' => $info->option_a,//A 21 'option_b' => $info->option_b,//B 22 'option_c' => $info->option_c,//C 23 'option_d' => $info->option_d,//D 24 'option_e' => $info->option_e,//E 25 'option_f' => $info->option_f,//F 26 'option_g' => $info->option_g,//G 27 'option_h' => $info->option_h,//H 28 'answer' => $info->answer,//正确答案 29 'pageid'=>$pageid, 30 ]; 31 $data=PoliceExam::create($arr1); 32 } 33 break; 34 case 1: 35 foreach ($value as $info){ 36 $arr1 = [ 37 'title' => $info->title,//题目 38 'answer' => $info->answer,//正确答案 39 'pageid'=>$pageid, 40 ]; 41 $data=PoliceExam::create($arr1); 42 } 43 break; 44 case 2: 45 foreach ($value as $info){ 46 $arr1 = [ 47 'title' => $info->title,//题目 48 'type' => $info->type,//题目类型 49 'option_a' => $info->option_a,//A 50 'option_b' => $info->option_b,//B 51 'option_c' => $info->option_c,//C 52 'option_d' => $info->option_d,//D 53 'option_e' => $info->option_e,//E 54 'answer' => $info->answer,//正确答案 55 'pageid'=>$pageid, 56 ]; 57 $data=PoliceExam::create($arr1); 58 } 59 break; 60 } 61 62 if($data){ 63 return ResponseLayout::apply(true,'数据录入成功',['data'=>$data]); 64 }else{ 65 return ResponseLayout::apply(false,'数据录入失败',['data'=>$data]); 66 } 67 }