php - 在 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);
}
解决方案
我设法通过使用 for 循环并使用另一个变量来接收初始数组的结果来解决这个问题:
for ($i = 0; $i < $limit; $i++) {
$insert = array(
'type_id' => $dataEnderecos['type_id'][$i],
// ^^^^ ^^^^ ^^^^
// Initial Array Secondary Array Index
);
}
推荐阅读
- amazon-web-services - AWS - 云形成 RDS 资源创建错误 - DBInstance 不稳定
- ios - UICollectionView 使用领域通知执行批量更新崩溃
- types - 水晶报表中 NUMBER 和 CURRENCY 类型的大小是多少?
- javascript - 当每个 CheckBoxes 值等于一定数量时显示/隐藏 Div
- oracle - 从 ojdbc6/jdk6 升级到 ojdbc8/jdk8 破坏了 TIMESTAMPTZWrapper。无法将 TIMESTAMPTZWrapper 强制转换为 TIMESTAMPTZ
- c - Gtk2 使用相同的按钮从 2 个条目中获取数据
- c# - 创建所有可能的对象状态的集合
- django - Django Google api 客户端库 oauth2client - 未解析的参考 FlowField
- angular - ngb-accordion 在设置 activeIds 时应用滚动/聚焦
- java - 无法通过浏览器访问控制器