【发布时间】:2019-01-05 18:23:31
【问题描述】:
在这个问题上已经做了很长一段时间了,我很困惑为什么使用 foreach 循环不起作用,但我展示的另一种方式确实有效。
我的代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://maps.googleapis.com/maps/api/geocode/json?address=1%20The%20Strand%20Wellard");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json"
));
$response = curl_exec($ch);
$json = json_decode($response, true);
foreach ($json['results'] as $item) {
foreach ($item['geometry'] as $item2) {
foreach ($item2['location'] as $item3) {
echo $item3['lat'];
}
}
echo $item['geometry']['location']['lat'];
}
curl_close($ch);
JSON 数据:https://pastebin.com/JU1wSpsD
为什么echo $item3['lat']; 不起作用但echo $item['geometry']['location']['lat']; 起作用?如果有人可以帮助我理解为什么会这样,那就太好了!
【问题讨论】:
-
添加json数据。提问。
-
这些答案都没有解决您的问题吗?