我的项目有一个由请求组成的数组,其中包含输入数组,如下所示:array:8 [▼ "type_id" => array:1 [▼ 0 => "1" ] "zip_code" => array:1 [▼ 0 => "88801500" ] "street_address" => array:1 [▼ 0 => "Avenida Getúlio Vargas" ] "number" => array:1 [▼ 0 => "asdasd" ] "street_address_2" => array:1 [▼ 0 => "asdasd" ] "city_id" => array:1 [▼ 0 => "4384" ] "neighborhood" => array:1 [▼ 0 => "Centro" ] "created_by" => 2]但是当我尝试在 foreach 上运行所述数组以将其插入数据库时,我得到的结果没有意义:array:1 [▼ 0 => "1"]我的代码:dd($dataEnderecos); //This is for debug purposes, it shows the initial array on this question. foreach ($dataEnderecos as $enderecos) { dd($enderecos); //This is for debug purposes, it shows the secondary array on this question. $enderecoID = $this->address->create($enderecos)->id; $this->repository->enderecos()->attach($enderecoID); }
1 回答
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
我设法通过使用 for 循环并使用另一个变量来接收初始数组的结果来解决此问题:
for ($i = 0; $i < $limit; $i++) {
$insert = array(
'type_id' => $dataEnderecos['type_id'][$i],
// ^^^^ ^^^^ ^^^^
// Initial Array Secondary Array Index
);
}
- 1 回答
- 0 关注
- 83 浏览
添加回答
举报
0/150
提交
取消