/ / jsonは "{"と "[" [closed] - php、jsonからデコードします

jsonは "{"と "[" [closed] - php、jsonからデコードします

私は次のようにjson_decodeしようとしています:

   "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

私が見ることができるのは、第二のものが余分な[[]を持っているという違いです

そのjsonから天気 - > idを取得する方法を教えてもらえますか、ありがとう

回答:

回答№1は4

weather要素は配列です。要素のリストを含んでいます。あなたが望むその正確な例からあなたが望むものを得るには:

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

また、その配列に複数の要素がある場合や、ゼロがある場合には、何を起こそうとしているのか考えたいと思うかもしれません。