I'm trying to get data from the following JSON file using PHP. I specifically want "temperatureMin" and "temperatureMax".
It's probably really simple, but I have no idea how to do this. I'm stuck on what to do after file_get_contents("file.json"). Some help would be greatly appreciated!
{
"daily": {
"summary": "No precipitation for the week; temperatures rising to 6° on Tuesday.",
"icon": "clear-day",
"data": [
{
"time": 1383458400,
"summary": "Mostly cloudy throughout the day.",
"icon": "partly-cloudy-day",
"sunriseTime": 1383491266,
"sunsetTime": 1383523844,
"temperatureMin": -3.46,
"temperatureMinTime": 1383544800,
"temperatureMax": -1.12,
"temperatureMaxTime": 1383458400,
}
]
}
}
Get the content of the JSON file using
file_get_contents()
:Now decode the JSON using
json_decode()
:You have an associative array containing all the information. To figure out how to access the values you need, you can do the following:
This will print out the contents of the array in a nice readable format. Note that the second parameter is set to
true
in order to letprint_r()
know that the output should be returned (rather than just printed to screen). Then, you access the elements you want, like so:Or loop through the array however you wish:
Demo!