获取JSON对象的一部分

时间:2011-08-05 02:21:51

标签: php json google-maps

我正在使用Google地图和地理编码(将地址转换为lang / lat坐标)。到目前为止一切正常,我从谷歌地图服务器得到一个文件的答案:

$address = urlencode( $address );
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}&sensor=false";
$content = file_get_contents( $url );
echo '<pre>';
    print_r( $content );
echo '</pre>';

重点是我不需要所有这些信息。

问题

A 如何只获取JSON对象的一部分?  (示例:我只需要'results'->'address_components'->'type' = 'country';。)

B 我可以以某种方式减少返回(谷歌)文件的内容,因为我不需要所有内容吗?

更新

我需要将部分数据添加到json对象中。所以解码&amp;然后编码似乎失去了能量和时间(此时)。任何想法,如果我要求更好(更快)请求XML对象? (最后一组对象非常大。这就是我真正关心性能的原因。)


来自Google Maps地图服务器的示例JSON答案(文件内容):

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "28",
               "short_name" : "28",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Sellhorner Weg",
               "short_name" : "Sellhorner Weg",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Behringen",
               "short_name" : "Behringen",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Bispingen",
               "short_name" : "Bispingen",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Soltau-Fallingbostel",
               "short_name" : "Soltau-Fallingbostel",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Niedersachsen",
               "short_name" : "NDS",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Deutschland",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "29646",
               "short_name" : "29646",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Sellhorner Weg 28, 29646 Bispingen, Deutschland",
         "geometry" : {
            "location" : {
               "lat" : 53.118080,
               "lng" : 9.966859999999999
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 53.11942898029150,
                  "lng" : 9.96820898029150
               },
               "southwest" : {
                  "lat" : 53.11673101970850,
                  "lng" : 9.965511019708496
               }
            }
         },
         "partial_match" : true,
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

1 个答案:

答案 0 :(得分:4)

$array = json_decode($content, true);
$countries = array_filter($array['results']['address_components'], function ($a) {
    return in_array('country', $a['types']);
});

var_dump($countries);

为您提供address_components类型的所有'country'。可能只会是一个,但可能不止一个。此语法需要PHP 5.3+ BTW。

  

我可以以某种方式减少返回(谷歌)文件的内容,因为我不需要所有内容吗?

只需取出您需要的已解码$array部分,然后忘记其余部分。如果您愿意,可以再次json_encode

相关问题