解析来自Google Geocode的json响应

时间:2017-08-13 06:22:31

标签: php arrays json google-maps google-api

我试图解析Google API的回复。

我想知道是否有一种简单/有效的方法可以在php中解析这个数组,以便从“地址组件”中获取数据。阵列。

{
   "results" : [
      {
     "address_components" : [
        {
           "long_name" : "10",
           "short_name" : "10",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Downing Street",
           "short_name" : "Downing St",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Westminster",
           "short_name" : "Westminster",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "London",
           "short_name" : "London",
           "types" : [ "postal_town" ]
        },
        {
           "long_name" : "Greater London",
           "short_name" : "Greater London",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "England",
           "short_name" : "England",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "SW1A 2AA",
           "short_name" : "SW1A 2AA",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "10 Downing St, Westminster, London SW1A 2AA, UK",
     "geometry" : {
        "location" : {
           "lat" : 51.5033635,
           "lng" : -0.1276248
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 51.5047124802915,
              "lng" : -0.126275819708498
           },
           "southwest" : {
              "lat" : 51.5020145197085,
              "lng" : -0.128973780291502
           }
        }
     },
     "place_id" : "ChIJRxzRQcUEdkgRGVaKyzmkgvg",
     "types" : [ "establishment", "point_of_interest" ]
  }
],
"status" : "OK"
}

我的初始方法是在数组中运行循环并创建一个新数组并将键设置为"类型"但是我遇到了一些问题,其中有多个选项用于&# 34;类型"

1 个答案:

答案 0 :(得分:1)

var contentRendered = '<p><img src="https://www.w3schools.com/css/paris.jpg" alt="w3c" />Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. <a href="htts://www.w3schools.com/css/paris">Link</a></P>'

// create a DOM object from the rendered text
var template = document.createElement("template");
template.innerHTML = contentRendered;

// use DOM API to retrieve the data you need from the object
var imgPart = template.content.firstChild.querySelector("img");
var text = template.content.firstChild.innerText;

console.log(imgPart);
console.log(text);

将输出:

$data='{
   "results" : [
      {
     "address_components" : [
        {
           "long_name" : "10",
           "short_name" : "10",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Downing Street",
           "short_name" : "Downing St",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Westminster",
           "short_name" : "Westminster",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "London",
           "short_name" : "London",
           "types" : [ "postal_town" ]
        },
        {
           "long_name" : "Greater London",
           "short_name" : "Greater London",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "England",
           "short_name" : "England",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "SW1A 2AA",
           "short_name" : "SW1A 2AA",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "10 Downing St, Westminster, London SW1A 2AA, UK",
     "geometry" : {
        "location" : {
           "lat" : 51.5033635,
           "lng" : -0.1276248
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 51.5047124802915,
              "lng" : -0.126275819708498
           },
           "southwest" : {
              "lat" : 51.5020145197085,
              "lng" : -0.128973780291502
           }
        }
     },
     "place_id" : "ChIJRxzRQcUEdkgRGVaKyzmkgvg",
     "types" : [ "establishment", "point_of_interest" ]
  }
],
"status" : "OK"
}';

$json=json_decode( $data );
if( $json->status=='OK' ){

    $data=array();/* store results */

    $results=$json->results;
    foreach( $results as $index => $obj ){
        $add=$obj->address_components;
        foreach( $add as $address ){
            $data[ $obj->formatted_address ][ implode( '-', array_values( $address->types ) ) ]=$address->long_name;
        }
    }
    echo '<pre>',print_r($data,1),'</pre>';
}