<?php
$json = '{
"results" : [
{
"address_components" : [
{
"long_name" : "301",
"short_name" : "301",
"types" : [ "subpremise" ]
},
{
"long_name" : "880",
"short_name" : "880",
"types" : [ "street_number" ]
},
{
"long_name" : "N Street",
"short_name" : "N St",
"types" : [ "route" ]
},
{
"long_name" : "South Addition",
"short_name" : "South Addition",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Anchorage",
"short_name" : "Anchorage",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Anchorage",
"short_name" : "Anchorage",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Alaska",
"short_name" : "AK",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "99501",
"short_name" : "99501",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "880 N Street #301, Anchorage, AK 99501, USA",
"geometry" : {
"location" : {
"lat" : 61.21391800000001,
"lng" : -149.908357
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 61.21526698029151,
"lng" : -149.9070080197085
},
"southwest" : {
"lat" : 61.2125690197085,
"lng" : -149.9097059802915
}
}
},
"partial_match" : true,
"types" : [ "subpremise" ]
}
],
"status" : "OK"
}';
function getAddress($elements){
$addr = [];
foreach($elements as $e){
if(isset($e['types']) && in_array("street_number",$e['types'])) $addr['street_number'] = $e['long_name'];
if(isset($e['types']) && in_array("street_number",$e['types'])) $addr['route'] = $e['long_name'];
if(isset($e['types']) && in_array("administrative_area_level_2",$e['types'])) $addr['city'] = $e['long_name'];
}
return $addr;
}
$obj = json_decode($json, true);
$c = $obj['results'][0]['address_components'];
var_dump(getAddress($c));
- Output for git.master, git.master_jit, rfc.property-hooks
- array(3) {
["street_number"]=>
string(3) "880"
["route"]=>
string(3) "880"
["city"]=>
string(9) "Anchorage"
}
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
32.14 ms | 401 KiB | 8 Q