Php json解码自;{quot;和[quot;];
我正在尝试解码以下内容:Php json解码自;{quot;和[quot;];,php,json,Php,Json,我正在尝试解码以下内容: "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, "mai
"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中获取weather->id吗,谢谢weather元素是一个数组:它包含一个元素列表。要从该确切示例中获取所需内容,您需要:
$id = $result->weather[0]->id;
如果该数组中有多个元素,或者没有元素,您可能还需要考虑希望发生什么。气象元素是一个数组:它包含元素列表。要从该确切示例中获得所需内容,您需要:
$id = $result->weather[0]->id;
您可能还想知道如果该数组中有多个元素,或者有零个元素,您希望发生什么。
[]
表示它是一个数组。因此它的元素具有id
,而不是id直接var\u dump($result)
-请查看您正在使用的内容。[]
是数组的一个附加层。所以我需要尝试这样的$result->weather->->->id?[/code>意味着它是一个数组。所以它的元素有一个id
,而不是一个id直接var\u dump($result)
-看看你在用什么。[]
是一个额外的数组层。所以我需要尝试这样的$result->weather->->->id?很好,谢谢。很好,谢谢