json解码来自“{”和“[”[关闭]

Im trying to json_decode the following:

   "main":{
      "temp":9.04,
      "temp_min":9.04,
      "temp_max":9.04,
      "pressure":938.13,
      "sea_level":1037.57,
      "grnd_level":938.13,
      "humidity":87
   },
   "weather":[
      {
         "id":801,
         "main":"Clouds",
         "description":"few clouds",
         "icon":"02d"
      }
   ],


$result = json_decode($json);
$temp = $result->main->temp; //is displaying the data
however
$id = $result->weather->id; //is not displaying anything

all i can see is difference that te second one have an extra "[]"

can you help me telling how can i get weather->id from that json, thank you

The weather element is an array: it contains a list of elements. To get what you want from that exact example you would want:

$id = $result->weather[0]->id;

You also might want to think about what you want to happen if there is more than one element in that array, or if there are zero.