Reputation: 548
I have a json file like this:
{"results" : [
{
"address_components" : [
{
"long_name" : "4400",
"short_name" : "4400",
"types" : [ "postal_code" ]
},
{
"long_name" : "Upper Austria",
"short_name" : "OÖ",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Austria",
"short_name" : "AT",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "4400, Austria",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.06669549999999,
"lng" : 14.4687485
},
"southwest" : {
"lat" : 47.9987654,
"lng" : 14.3408425
}
},
"location" : {
"lat" : 48.0418838,
"lng" : 14.4078882
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.06669549999999,
"lng" : 14.4687485
},
"southwest" : {
"lat" : 47.9987654,
"lng" : 14.3408425
}
}
},
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
and everytime i try to access it i get undefined index location, i want to access this part:
"location" : {
"lat" : 48.0418838,
"lng" : 14.4078882
},
with this:
$url1="http://maps.googleapis.com/maps/api/geocode/json?address=AT-".$a."&sensor=false";
$unparsed_json = file_get_contents($url1);
$data =(array) json_decode($unparsed_json);
echo $unparsed_json;
$latlng= $data['results']['location']['lat'];
echo $latlng;
but everything I try gives result null. Any help would be really appreciated.
Upvotes: 0
Views: 67
Reputation: 429
You are using arrays in your json code and need to reference items accordingly, also your location is located in the geometry entity.
Try out with this $latlng= $data['results'][0]['geometry']['location']['lat'];
Here's even a demo in JS just to show the concept. Use the line above in your PHP code.
Upvotes: 1