Search code examples
phpjsongoogle-elevation-api

Get Value of PHP Array from Google Elevation API


I am developing a system to get values from gGoogle Api Elevation with PHP. I have it working and when I get the value it returns as String in this format:

{ "results" : [ { "elevation" : 914.033935546875, "location" : { "lat" : -25.4499861, "lng" : -49.2339611 }, "resolution" : 152.7032318115234 } ], "status" : "OK" }       

I want to get the value of the elevation in this result, can you help me to do this? thanks you.


Solution

  • The string is in JSON format.

    You can use json_decode to convert it to a PHP array.

    $json_string = <<<JSON
    {
      "results": [
        {
          "elevation": 914.033935546875,
          "location": {
            "lat": -25.4499861,
            "lng": -49.2339611
          },
          "resolution": 152.7032318115234
        }
      ],
      "status": "OK"
    }
    JSON;
    
    $array = json_decode($json_string, TRUE);
    echo $array['results'][0]['elevation'];
    

    Output:

    914.03393554688