Php 在laravel中拆分数组并插入到数据库
我有这样的数组Php 在laravel中拆分数组并插入到数据库,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有这样的数组 $group=[['abc','cde','01/03/2019','01/05/2019'],['123','456','01/23/2019','01/30/2019']]; 我想按4字段插入数据库,如下所示: field1 field2 field3 field4 abc cde 01/03/2019 01/05/2019 123 456 01/23/2019 01/30/20
$group=[['abc','cde','01/03/2019','01/05/2019'],['123','456','01/23/2019','01/30/2019']];
我想按4字段插入数据库,如下所示:
field1 field2 field3 field4
abc cde 01/03/2019 01/05/2019
123 456 01/23/2019 01/30/2019
我尝试以下代码:
for($j=1;$j<count($groups);$j++){
for ($k=0; $k < count($groups[$j]) ; $k++) {
$insert = array(
'activity' => $groups[$j][$k],
'person' => $groups[$j][$k],
'startdate' => $groups[$j][$k],
'finishdate' => $groups[$j][$k]
);
DB::table('test_tbl')->insert($insert);
}
}
对于($j=1;$j$组[$j][$k],
“个人”=>$groups[$j][$k],
“startdate”=>$groups[$j][$k],
“finishdate”=>$groups[$j][$k]
);
DB::table('test_tbl')->insert($insert);
}
}
谢谢你,我认为在这种情况下最简单的方法就是硬编码索引 像这样:
for($j = 0; $j < count($groups); $j++){
$insert = array(
'activity' => $groups[$j][0],
'person' => $groups[$j][1],
'startdate' => $groups[$j][2],
'finishdate' => $groups[$j][3]
);
DB::table('test_tbl')->insert($insert);
}
for($j=0;$j$groups[$j][0],
“个人”=>$groups[$j][1],
“startdate”=>$groups[$j][2],
“finishdate”=>$groups[$j][3]
);
DB::table('test_tbl')->insert($insert);
}
在FOR循环中,索引计数器
$j
通常从0开始。所以在这方面要小心。我认为在这种情况下,最简单的方法就是硬编码索引
像这样:
for($j = 0; $j < count($groups); $j++){
$insert = array(
'activity' => $groups[$j][0],
'person' => $groups[$j][1],
'startdate' => $groups[$j][2],
'finishdate' => $groups[$j][3]
);
DB::table('test_tbl')->insert($insert);
}
for($j=0;$j$groups[$j][0],
“个人”=>$groups[$j][1],
“startdate”=>$groups[$j][2],
“finishdate”=>$groups[$j][3]
);
DB::table('test_tbl')->insert($insert);
}
在FOR循环中,索引计数器$j
通常从0开始。所以在这方面要小心。foreach怎么样
foreach ($group as $item){
$insert = array(
'activity' => $item[0],
'person' => $item[1],
'startdate' => $item[2],
'finishdate' => $item[3]
);
DB::table('test_tbl')->insert($insert);
}
}
foreach怎么样
foreach ($group as $item){
$insert = array(
'activity' => $item[0],
'person' => $item[1],
'startdate' => $item[2],
'finishdate' => $item[3]
);
DB::table('test_tbl')->insert($insert);
}
}
弗雷奇
我建议您使用ForEach循环:
foreach($group as $item){
$insert = array(
'activity' => $item[0],
'person' => $item[1],
'startdate' => $item[2],
'finishdate' => $item[3]
);
DB::table('test_tbl')->insert($insert);
}
这样,您就不需要计算数组,代码也很整洁。Foreach
我建议您使用ForEach循环:
foreach($group as $item){
$insert = array(
'activity' => $item[0],
'person' => $item[1],
'startdate' => $item[2],
'finishdate' => $item[3]
);
DB::table('test_tbl')->insert($insert);
}
这样,您就不需要计算数组,代码也很整洁。使用
使用
是的,没问题,希望有帮助~干杯是的,没问题,希望有帮助~干杯