I'm trying to access JSON data with PHP. I have the following JSON data from an API stored as $json
.
{
"lat": 33.44,
"lon": -94.04,
"timezone": "America\/Chicago",
"timezone_offset": -21600,
"current": {
"dt": 1609797727,
"sunrise": 1609766440,
"sunset": 1609802491,
"temp": 66.2,
"feels_like": 55.81,
"pressure": 1018,
"humidity": 20,
"dew_point": 24.91,
"uvi": 0.4,
"clouds": 1,
"visibility": 10000,
"wind_speed": 10.29,
"wind_deg": 300,
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
]
}
I'm trying to access each item in it. When I try "echo $json["lat"];
", for example, I don't get anything.
When I try:
foreach($json as $data){
echo $data;
echo "<br>";
}
I get:
33.44
-94.04
America/Chicago
-21600
Why isn't echo $json["lat"];
giving 33.44, and why doesn't echo $json["current"]["temp"];
giving 66.2? How do I access the data correctly?