Question

I am having trouble targeting the particular value in the array. I am trying to target the 'lng' and 'lat'. I feel like I'm close, and have tried several different ways to target. Currently trying this:

$json = file_get_contents($jsonurl);
$output = json_decode($json, true);

$latitude=$output['results']['locations']['latLng'][0];
$longitude=$output['results']['locations']['latLng'][1];

The JSON is:

array(3) {
  ["results"]=>
  array(1) {
    [0]=>
    array(2) {
      ["locations"]=>
      array(1) {
        [0]=>
        array(19) {
          ["latLng"]=>
          array(2) {
            ["lng"]=>
            float(-122.5008)
            ["lat"]=>
            float(47.2629)
          }
          ["adminArea4"]=>
          string(13) "Pierce County"
          ["adminArea5Type"]=>
          string(4) "City"
          ["adminArea4Type"]=>
          string(6) "County"
          ["adminArea5"]=>
          string(6) "Tacoma"
          ["street"]=>
          string(0) ""
          ["adminArea1"]=>
          string(2) "US"
          ["adminArea3"]=>
          string(2) "WA"
          ["type"]=>
          string(1) "s"
          ["displayLatLng"]=>
          array(2) {
            ["lng"]=>
            float(-122.5008)
            ["lat"]=>
            float(47.2629)
          }
          ["linkId"]=>
          int(0)
          ["postalCode"]=>
          string(5) "98406"
          ["sideOfStreet"]=>
          string(1) "N"
          ["dragPoint"]=>
          bool(false)
          ["adminArea1Type"]=>
          string(7) "Country"
          ["geocodeQuality"]=>
          string(3) "ZIP"
          ["geocodeQualityCode"]=>
          string(5) "Z1XAA"
          ["mapUrl"]=>
          string(186) "http://www.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luub2gu2ll,7w=o5-9uaauz&type=map&size=225,160&pois=purple-1,47.2629,-122.5008,0,0|&center=47.2629,-122.5008&zoom=12&rand=-8386350"
          ["adminArea3Type"]=>
          string(5) "State"
        }
      }
      ["providedLocation"]=>
      array(1) {
        ["location"]=>
        string(5) "98406"
      }
    }
  }
  ["options"]=>
  array(3) {
    ["ignoreLatLngInput"]=>
    bool(false)
    ["maxResults"]=>
    int(-1)
    ["thumbMaps"]=>
    bool(true)
  }
  ["info"]=>
  array(3) {
    ["copyright"]=>
    array(3) {
      ["text"]=>
      string(22) "© 2013 MapQuest, Inc."
      ["imageUrl"]=>
      string(35) "http://api.mqcdn.com/res/mqlogo.gif"
      ["imageAltText"]=>
      string(22) "© 2013 MapQuest, Inc."
    }
    ["statuscode"]=>
    int(0)
    ["messages"]=>
    array(0) {
    }
  }
}
Was it helpful?

Solution

Try

$latitude=$output['results'][0]['locations'][0]['latLng']['lat'];
$longitude=$output['results'][0]['locations'][0]['latLng']['lng'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top