我正在尝试从Google地理编码功能返回的JSON响应中访问数据。如何在以下JSON中访问邮政编码'L13'(来自address_components部分的long_name)
到目前为止我的最大努力是在下面(输出一个数组);
public double getDistance(double[] points1, double[] points2){
double x1 = points1[0];
double x2 = points2[0];
double y1 = points1[1];
double y2 = points2[1];
double z1 = points1[2];
double z2 = points2[2];
double distance = Math.sqrt(Math.pow(x1 - x2, 2) +(Math.pow(y1 - y2, 2) +
(Math.pow(z1 - z2, 2))));
return distance;
}
JSON响应;
$address = 'Old Swan';
$coordinates = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '®ion=gb');
$coordinates = json_decode($coordinates);
echo $coordinates->results[0]->address_components;
答案 0 :(得分:0)
您可以使用数组搜索来查找所选数组;
PHP有一个允许你通过数组寻找的功能
http://php.net/manual/en/function.array-search.php
这是一个例子
$result = array_search("L3", array_column($coordinates->results[0]->address_components, 'long_name'));