在 Json(嵌套数组)PHP 中搜索键
Search a key in a Json (nested array) PHP
我使用 WU api 来获取一些每小时的天气数据。 API returns 具有多个小时值的 Json 我想查询我需要的那个:
Json:
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"hourly": 1
}
}
,
"hourly_forecast": [
{
"FCTTIME": {
"hour": "13","hour_padded": "13","min": "00","min_unpadded": "0","sec": "0","year": "2015","mon": "12","mon_padded": "12","mon_abbrev": "Dec","mday": "30","mday_padded": "30","yday": "363","isdst": "0","epoch": "1451509200","pretty": "1:00 PM PST on December 30, 2015","civil": "1:00 PM","month_name": "December","month_name_abbrev": "Dec","weekday_name": "Wednesday","weekday_name_night": "Wednesday Night","weekday_name_abbrev": "Wed","weekday_name_unlang": "Wednesday","weekday_name_night_unlang": "Wednesday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
},
"temp": {"english": "60", "metric": "16"},
"dewpoint": {"english": "34", "metric": "1"},
"condition": "Clear",
"icon": "clear"
}
,
{
"FCTTIME": {
...
}
,
"FCTTIME": {
...
},
... more FCTTIME
]
}
这是我获取 hourly_forecast
作为数组的代码:
$jsonRequest = file_get_contents("weather.json");
$data = json_decode($jsonRequest, true);
$array = @$data['hourly_forecast'];
我需要为 "hour": "13"
获取 temp
和 condition
。所以 PHP 会打印出如下内容: The weather is $condition with $temp degree.
您应该能够遍历一组每小时的预测,当您找到与您正在寻找的小时相匹配的预测时,从该预测中获取您想要的值,然后跳出循环。
foreach ($data['hourly_forecast'] as $forecast) {
if ($forecast['FCTTIME']['hour'] == 13) {
$condition = $forecast['condition'];
$temp = $forecast['temp'];
break;
}
}
首先不要将对象转为数组,没有必要。
所以改变$data = json_decode($jsonRequest, true);
到$data = json_decode($jsonRequest);
然后遍历 $data->hourly_forecast
数组寻找你需要的特定时间,就像这样
foreach( $data->hourly_forecast as $forecast ) {
if ( $forecast->FCTTIME->hour == 13 ) {
echo 'The time ' . $forecast->FCTTIME->hour.PHP_EOL;
echo 'The temp F' . $forecast->temp->english.PHP_EOL;
echo 'The temp C' . $forecast->temp->metric.PHP_EOL;
echo 'The condition ' . $forecast->condition.PHP_EOL;
// if this is all you need, stop looping
break;
}
}
我使用 WU api 来获取一些每小时的天气数据。 API returns 具有多个小时值的 Json 我想查询我需要的那个:
Json:
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"hourly": 1
}
}
,
"hourly_forecast": [
{
"FCTTIME": {
"hour": "13","hour_padded": "13","min": "00","min_unpadded": "0","sec": "0","year": "2015","mon": "12","mon_padded": "12","mon_abbrev": "Dec","mday": "30","mday_padded": "30","yday": "363","isdst": "0","epoch": "1451509200","pretty": "1:00 PM PST on December 30, 2015","civil": "1:00 PM","month_name": "December","month_name_abbrev": "Dec","weekday_name": "Wednesday","weekday_name_night": "Wednesday Night","weekday_name_abbrev": "Wed","weekday_name_unlang": "Wednesday","weekday_name_night_unlang": "Wednesday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
},
"temp": {"english": "60", "metric": "16"},
"dewpoint": {"english": "34", "metric": "1"},
"condition": "Clear",
"icon": "clear"
}
,
{
"FCTTIME": {
...
}
,
"FCTTIME": {
...
},
... more FCTTIME
]
}
这是我获取 hourly_forecast
作为数组的代码:
$jsonRequest = file_get_contents("weather.json");
$data = json_decode($jsonRequest, true);
$array = @$data['hourly_forecast'];
我需要为 "hour": "13"
获取 temp
和 condition
。所以 PHP 会打印出如下内容: The weather is $condition with $temp degree.
您应该能够遍历一组每小时的预测,当您找到与您正在寻找的小时相匹配的预测时,从该预测中获取您想要的值,然后跳出循环。
foreach ($data['hourly_forecast'] as $forecast) {
if ($forecast['FCTTIME']['hour'] == 13) {
$condition = $forecast['condition'];
$temp = $forecast['temp'];
break;
}
}
首先不要将对象转为数组,没有必要。
所以改变$data = json_decode($jsonRequest, true);
到$data = json_decode($jsonRequest);
然后遍历 $data->hourly_forecast
数组寻找你需要的特定时间,就像这样
foreach( $data->hourly_forecast as $forecast ) {
if ( $forecast->FCTTIME->hour == 13 ) {
echo 'The time ' . $forecast->FCTTIME->hour.PHP_EOL;
echo 'The temp F' . $forecast->temp->english.PHP_EOL;
echo 'The temp C' . $forecast->temp->metric.PHP_EOL;
echo 'The condition ' . $forecast->condition.PHP_EOL;
// if this is all you need, stop looping
break;
}
}