Analyze the Google Geocode json response

Advertisement

I'm trying to analyze a response from a Google API.

I wonder if there is an easy / efficient way to parse this table in php to get the data from the 'address_components' array.

 { "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" } 

My initial method was to run a loop in the array and create a new array and set the key on the "types", but I encountered some problems where there was more than one option for "types"

The answer

 $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 ' 
  ', print_r ($ data, 1),' 
'; }

Exit:

 Array ( [10 Downing St, Westminster, London SW1A 2AA, UK] => Array ( [street_number] => 10 [route] => Downing Street [neighborhood-political] => Westminster [postal_town] => London [administrative_area_level_2-political] => Greater London [administrative_area_level_1-political] => England [country-political] => United Kingdom [postal_code] => SW1A 2AA ) )