为了账号安全,请及时绑定邮箱和手机立即绑定

在 foreach 中运行时,数组的数组返回无意义

在 foreach 中运行时,数组的数组返回无意义

PHP
慕婉清6462132 2023-07-01 15:08:59
我的项目有一个由请求组成的数组,其中包含输入数组,如下所示: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

   );

}


查看完整回答
反对 回复 2023-07-01
  • 1 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信