0

I unfortunately got some problems getting the exact information I want out of this json file.

Thats a part of the json file:

{
   "weather":[
      {
         "id":600,
         "main":"Snow",
         "description":"light snow",
         "icon":"13n"
      }
   ],
   "base":"stations",
   "main":{
      "temp":271.15,
      "feels_like":266.48,
      "temp_min":271.15,
      "temp_max":271.15,
      "pressure":1020,
      "humidity":86
   }
}

Thats my code to read the informations:

$weather = file_get_contents("weather.json");
$weather = json_decode($weather);
                
foreach($weather->main as $main) { //<-- This dosen't works
     echo $main->temp;
}
foreach($weather->weather as $w) { //<-- This works
     echo $w->description;
}

I see that there is a different between the weather- and the main-part with the "[...]" but I don't know how to handle that. Thank you for everyone who helps! :)

Camilo
  • 6,504
  • 4
  • 39
  • 60
Kai Zobel
  • 13
  • 4

1 Answers1

0
foreach($weather->weather as $w) { 
     echo $w->description;
}

As per Json weather works because it has array of objects so by looping it by foreach you get an object and can access properties there

foreach($weather->main as $main) { 
     echo $main->temp;
}

Where main is object not array of object so while looping it not showing desired results Though you can access its value as

$weather->main->temp

Please look to the main and make it like

$array[‘weather’][‘main’][]= main object here That should work