Iterate over objects returned from Google Maps API

See here for the original answer.

kshetline answered your question the way you would interact with the JSON object in JavaScript, but since you never mentioned JavaScript and only have PHP in your question's tags I am going to try to answer you with PHP.

To access properties of a JSON object in PHP, you must use the arrow syntax:

$result = $djd->results;
for($i=0; $i<5; $i++){
    // Do what you want with them from here
    $lat = $result[$i]->geometry->location->lat;
    $lng = $result[$i]->geometry->location->lng;

    // Example of how to return them
    echo json_encode($lat);
    echo json_encode($lng);
}

I do not know what the JSON returned looks like, so I based my PHP off of the JSON in kshetline's answer. I'm sure their JSON is correct, so this should work.


Tags

  1. php (Private)
  2. json (Private)
  3. google-maps (Private)
  4. stack-overflow (Private)
  5. answer (Private)