猿问

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

我的项目有一个由请求组成的数组,其中包含输入数组,如下所示:


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);

        }


慕婉清6462132
浏览 84回答 1
1回答

蝴蝶不菲

我设法通过使用 for 循环并使用另一个变量来接收初始数组的结果来解决此问题:for ($i = 0; $i < $limit; $i++) {&nbsp; &nbsp;$insert = array(&nbsp; &nbsp; &nbsp; 'type_id'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =>&nbsp; &nbsp;$dataEnderecos['type_id'][$i],&nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^&nbsp; &nbsp;^^^^&nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Initial Array&nbsp; Secondary Array Index&nbsp; &nbsp;);}
随时随地看视频慕课网APP
我要回答