Parsing Google Geocoding JSON with PHP

Lee Price picture Lee Price · May 24, 2012 · Viewed 20.9k times · Source

I'm trying to parse the json response from the Google Geocode API but I'm having a little trouble understanding it.

For those unfamiliar with the Geocode API here is the URL: http://maps.google.com/maps/api/geocode/json?address=albert%20square&sensor=false

I'm using the following code to parse the request

<?php

$address = urlencode($_POST['address']);
$request = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=" . $address . "&sensor=false");
$json = json_decode($request, true);

?>

And I'm trying to output with the following:

echo $json['results'][0]['formated_address'];

I'm not sure why nothing is being echoed. I've also tried $json[0]['results'][0]['formated_address']. I know this is a noob question, but multi-dimensional arrays confuse me.

Answer

DaveRandom picture DaveRandom · May 24, 2012
echo $json['results'][0]['formatted_address'];

It helps if you spell it correctly ;-)