【发布时间】:2017-12-26 21:37:55
【问题描述】:
我一直在到处寻找答案,没有找到。我正在使用谷歌地图地理定位 api 来显示经纬度的地址。我只需要显示选择性数据,例如: "formatted_address" 来自数组。这是我尝试在 PHP 中显示响应的代码:
$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875&sensor=false", true);
$data = json_decode($file_contents, true);
foreach($data as $output => $var) {
echo $var;
}
这是回复:[https://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875]
这是我得到的输出:
注意:C:\xampp\htdocs\bikewala\myaccount\index.php 行 176
数组确定
当我尝试使用此代码访问数组的任何组件时:
$file_contents = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?latlng=31.334097,74.235875&sensor=false", true);
$data = json_decode($file_contents, true);
foreach($data as $output => $var) {
echo $var['formatted_address'];
}
我收到此错误:
通知:未定义的索引:C:\xampp\htdocs\bikewala\myaccount\index.php 中的 formatted_address 在第 176 行
警告:C:\xampp\htdocs\bikewala\myaccount\index.php 中的非法字符串偏移 'formatted_address' 在第 176 行
哦
我在这里做错了什么?
提前致谢!
【问题讨论】:
标签: php arrays json google-maps google-geolocation