Disco Panda Disco Panda - 6 months ago 19
PHP Question

json_decode can't display array

OK I have looked at a few examples and I can't seem to display the array or an element. It keeps coming back as null-. I have the following JSON (from mapquest)

renderGeocode({
results: [
{
locations: [
{
latLng: {
lng: -90.1978,
lat: 38.627201
},
adminArea4: "Saint Louis City",
adminArea5Type: "City",
adminArea4Type: "County",
adminArea5: "Saint Louis",
street: "",
adminArea1: "US",
adminArea3: "MO",
type: "s",
displayLatLng: {
lng: -90.1978,
lat: 38.627201
},
linkId: 0,
postalCode: "",
sideOfStreet: "N",
dragPoint: false,
adminArea1Type: "Country",
geocodeQuality: "CITY",
geocodeQualityCode: "A5XAX",
mapUrl: "http://www.mapquestapi.com/staticmap/v4/getmap?key=123456789&type=map&size=225,160&pois=purple-1,38.627201,-90.1978,0,0|&center=38.627201,-90.1978&zoom=12&rand=1390479880",
adminArea3Type: "State"
}
],
providedLocation: {
location: "SAint Louis,mo"
}
}
],
options: {
ignoreLatLngInput: false,
maxResults: -1,
thumbMaps: true
},
info: {
copyright: {
text: "© 2013 MapQuest, Inc.",
imageUrl: "http://api.mqcdn.com/res/mqlogo.gif",
imageAltText: "© 2013 MapQuest, Inc."
},
statuscode: 0,
messages: [

]
}
})


I have attempted to add it to an array like so

$array =json_decode($data, true);


However no matter how I try to print or check the contents it all comes back as null I really am just trying to print out the lat lng and adminArea5.
Any help would be great.

Here is the full code

<?php
$where = filter_input(INPUT_GET, 'where', FILTER_SANITIZE_STRING);
$source = "getLat";
if ($source =="getLat")
{
ob_start();
$getsource = array('location' =>$where,
'type'=>$getWhat,
'callback'=>'ResultSet'
);
$url = "http://www.mapquestapi.com/geocoding/v1/address?key=123456789" . http_build_query($getsource, '', "&");
//print_r($url);
$data_mapquest = file_get_contents($url);
$array = json_decode($data_mapquest, true);
ob_end_flush();
}
?>

Answer

Don't include the 'callback' => 'ResultSet' parameter, that makes the response a JSONP response.

Comments