如何从 json weather.com 文件中获取数据并在 php 中显示结果



我有一个来自 weather.com 的json文件。如何从此列"TO DAY"中获取数据并通过 php 显示。

{
"id": "21.70,104.88",
"vt1dailyforecast": {
"validDate": ["2018-08-13T07:00:00+0700", "2018-08-14T07:00:00+0700", "2018-08-15T07:00:00+0700", "2018-08-16T07:00:00+0700", "2018-08-17T07:00:00+0700", "2018-08-18T07:00:00+0700", "2018-08-19T07:00:00+0700", "2018-08-20T07:00:00+0700", "2018-08-21T07:00:00+0700", "2018-08-22T07:00:00+0700", "2018-08-23T07:00:00+0700", "2018-08-24T07:00:00+0700", "2018-08-25T07:00:00+0700", "2018-08-26T07:00:00+0700", "2018-08-27T07:00:00+0700"],
"sunrise": ["2018-08-13T05:37:18+0700", "2018-08-14T05:37:39+0700", "2018-08-15T05:38:00+0700", "2018-08-16T05:38:21+0700", "2018-08-17T05:38:41+0700", "2018-08-18T05:39:01+0700", "2018-08-19T05:39:21+0700", "2018-08-20T05:39:40+0700", "2018-08-21T05:40:00+0700", "2018-08-22T05:40:19+0700", "2018-08-23T05:40:38+0700", "2018-08-24T05:40:56+0700", "2018-08-25T05:41:14+0700", "2018-08-26T05:41:33+0700", "2018-08-27T05:41:51+0700"],
"sunset": ["2018-08-13T18:33:04+0700", "2018-08-14T18:32:21+0700", "2018-08-15T18:31:38+0700", "2018-08-16T18:30:53+0700", "2018-08-17T18:30:07+0700", "2018-08-18T18:29:21+0700", "2018-08-19T18:28:34+0700", "2018-08-20T18:27:46+0700", "2018-08-21T18:26:58+0700", "2018-08-22T18:26:09+0700", "2018-08-23T18:25:19+0700", "2018-08-24T18:24:28+0700", "2018-08-25T18:23:37+0700", "2018-08-26T18:22:45+0700", "2018-08-27T18:21:53+0700"],
"moonIcon": ["WXC", "WXC", "WXC", "WXC", "WXC", "FQ", "WXG", "WXG", "WXG", "WXG", "WXG", "WXG", "WXG", "F", "F"],
"moonPhrase": ["Waxing Crescent", "Waxing Crescent", "Waxing Crescent", "Waxing Crescent", "Waxing Crescent", "First Quarter", "Waxing Gibbous", "Waxing Gibbous", "Waxing Gibbous", "Waxing Gibbous", "Waxing Gibbous", "Waxing Gibbous", "Waxing Gibbous", "Full Moon", "Full Moon"],
"moonrise": ["2018-08-13T07:20:15+0700", "2018-08-14T08:22:42+0700", "2018-08-15T09:21:39+0700", "2018-08-16T10:19:15+0700", "2018-08-17T11:14:22+0700", "2018-08-18T12:08:52+0700", "2018-08-19T13:01:55+0700", "2018-08-20T13:53:58+0700", "2018-08-21T14:44:56+0700", "2018-08-22T15:33:22+0700", "2018-08-23T16:20:28+0700", "2018-08-24T17:04:24+0700", "2018-08-25T17:46:06+0700", "2018-08-26T18:26:02+0700", "2018-08-27T19:03:39+0700"],
"moonset": ["2018-08-13T20:14:44+0700", "2018-08-14T20:58:09+0700", "2018-08-15T21:39:20+0700", "2018-08-16T22:20:10+0700", "2018-08-17T23:00:28+0700", "2018-08-18T23:41:40+0700", "", "2018-08-20T00:24:58+0700", "2018-08-21T01:09:26+0700", "2018-08-22T01:56:23+0700", "2018-08-23T02:45:13+0700", "2018-08-24T03:34:42+0700", "2018-08-25T04:25:49+0700", "2018-08-26T05:16:20+0700", "2018-08-27T06:07:39+0700"],
"dayOfWeek": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday"],
"snowQpf": [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
"day": {
"dayPartName": ["Today", "Tomorrow", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday"],
"precipPct": [10, 10, 20, 100, 100, 80, 80, 60, 40, 30, 80, 80, 80, 60, 80],
"precipAmt": [0.0, 0.0, 0.0, 0.73, 0.73, 0.3, 0.13, 0.11, 0.05, 0.02, 0.13, 0.2, 0.27, 0.14, 0.17],
"precipType": ["rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain"],
"temperature": [96, 97, 94, 87, 84, 87, 89, 90, 90, 89, 89, 89, 90, 89, 90],
"uvIndex": [10, 11, 11, 6, 8, 10, 11, 10, 10, 8, 8, 8, 8, 8, 8],
"uvDescription": ["Very High", "Extreme", "Extreme", "High", "Very High", "Very High", "Extreme", "Very High", "Very High", "Very High", "Very High", "Very High", "Very High", "Very High", "Very High"],
"icon": [30, 30, 30, 4, 4, 4, 4, 38, 38, 37, 4, 4, 4, 38, 4],
"iconExtended": [3000, 3000, 3000, 400, 400, 400, 400, 3800, 3800, 3700, 400, 400, 400, 3800, 400],
"phrase": ["Partly Cloudy", "Partly Cloudy", "Partly Cloudy", "Thunderstorms", "Thunderstorms", "Thunderstorms", "Thunderstorms", "Scattered Thunderstorms", "Scattered Thunderstorms", "Isolated Thunderstorms", "Thunderstorms", "Thunderstorms", "Thunderstorms", "Scattered Thunderstorms", "Thunderstorms"],
"narrative": ["Partly cloudy. High 96F. Winds light and variable.", "Partly cloudy. High 97F. Winds light and variable.", "Mostly cloudy skies early will become partly cloudy later in the day. High 94F. Winds light and variable.", "Thunderstorms likely. High 87F. Winds NW at 5 to 10 mph. Chance of rain 100%.", "Thunderstorms likely. High 84F. Winds NNE at 5 to 10 mph. Chance of rain 100%.", "Showers and thunderstorms. High 87F. Winds light and variable. Chance of rain 80%.", "Scattered thunderstorms in the morning, then mainly cloudy during the afternoon with thunderstorms likely. High 89F. Winds light and variable. Chance of rain 80%.", "Partly to mostly cloudy with a chance of thunderstorms. High near 90F. Winds light and variable. Chance of rain 60%.", "Scattered showers and thunderstorms, especially early in the day. High near 90F. Winds light and variable. Chance of rain 40%.", "Isolated thunderstorms in the morning, then mainly cloudy late. High 89F. Winds light and variable. Chance of rain 30%.", "Thunderstorms. High 89F. Winds light and variable. Chance of rain 80%.", "Thunderstorms likely. High 89F. Winds light and variable. Chance of rain 80%.", "Thunderstorms. High around 90F. Winds light and variable. Chance of rain 80%.", "Scattered thunderstorms. High 89F. Winds light and variable. Chance of rain 60%.", "Scattered thunderstorms in the morning becoming more widespread in the afternoon. High near 90F. Winds light and variable. Chance of rain 80%."],
"cloudPct": [36, 56, 61, 94, 85, 78, 67, 66, 71, 72, 79, 83, 82, 83, 85],
"windDirCompass": ["S", "NW", "NNW", "NW", "NNE", "N", "NNE", "ENE", "ENE", "ENE", "NE", "N", "NW", "NNW", "N"],
"windDirDegrees": [177, 318, 328, 318, 14, 1, 13, 73, 58, 58, 36, 357, 322, 337, 3],
"windSpeed": [5, 4, 4, 6, 6, 5, 4, 5, 4, 4, 4, 4, 4, 4, 4],
"humidityPct": [57, 64, 71, 82, 87, 81, 78, 78, 78, 80, 79, 79, 78, 77, 76],
"qualifier": [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
"snowRange": ["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
"thunderEnum": [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
"thunderEnumPhrase": ["No thunder", "No thunder", "No thunder", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected"]
},
"night": {
"dayPartName": ["Tonight", "Tomorrow night", "Wednesday night", "Thursday night", "Friday night", "Saturday night", "Sunday night", "Monday night", "Tuesday night", "Wednesday night", "Thursday night", "Friday night", "Saturday night", "Sunday night", "Monday night"],
"precipPct": [10, 20, 90, 100, 90, 80, 80, 80, 80, 80, 80, 80, 80, 40, 50],
"precipAmt": [0.0, 0.0, 0.37, 1.26, 0.79, 0.34, 0.31, 0.24, 0.25, 0.15, 0.14, 0.21, 0.25, 0.07, 0.12],
"precipType": ["rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain", "rain"],
"temperature": [79, 79, 78, 77, 77, 77, 77, 78, 77, 77, 77, 77, 77, 78, 77],
"uvIndex": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
"uvDescription": ["Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low", "Low"],
"icon": [29, 29, 4, 12, 4, 4, 4, 4, 4, 4, 4, 4, 4, 47, 47],
"iconExtended": [2900, 2900, 400, 1240, 400, 400, 400, 400, 400, 400, 400, 400, 400, 3809, 3809],
"phrase": ["Partly Cloudy", "Partly Cloudy", "Thunderstorms", "Rain/Thunder", "Thunderstorms", "Thunderstorms", "Thunderstorms", "Thunderstorms", "Thunderstorms", "Thunderstorms", "Thunderstorms", "Thunderstorms", "Thunderstorms", "Scattered Thunderstorms", "Scattered Thunderstorms"],
"narrative": ["Partly cloudy skies. Low 79F. Winds light and variable.", "A few clouds. Low 79F. Winds light and variable.", "Thunderstorms likely. Low 78F. Winds light and variable. Chance of rain 90%.", "Showers and thundershowers in the evening, then cloudy with rain likely overnight. Low 77F. Winds N at 5 to 10 mph. Chance of rain 100%. 1 to 2 inches of rain expected.", "Showers and thunderstorms likely. Low 77F. Winds light and variable. Chance of rain 90%.", "Showers and thunderstorms likely. Low 77F. Winds light and variable. Chance of rain 80%.", "Showers and thunderstorms likely. Low 77F. Winds light and variable. Chance of rain 80%.", "Thunderstorms. Low 78F. Winds light and variable. Chance of rain 80%.", "Thunderstorms likely. Low 77F. Winds light and variable. Chance of rain 80%.", "Thunderstorms. Low 77F. Winds light and variable. Chance of rain 80%.", "Thunderstorms likely. Low 77F. Winds light and variable. Chance of rain 80%.", "Thunderstorms likely. Low 77F. Winds light and variable. Chance of rain 80%.", "Thunderstorms likely. Low 77F. Winds light and variable. Chance of rain 80%.", "Variably cloudy with scattered thunderstorms. Low 78F. Winds light and variable. Chance of rain 40%.", "Scattered thunderstorms. Low 77F. Winds light and variable. Chance of rain 50%."],
"cloudPct": [44, 54, 75, 99, 95, 84, 86, 84, 77, 84, 83, 89, 83, 89, 89],
"windDirCompass": ["WNW", "WNW", "NW", "N", "NNW", "NNW", "N", "NNE", "NNE", "N", "NNE", "N", "N", "NNW", "NNE"],
"windDirDegrees": [296, 301, 308, 1, 336, 345, 350, 27, 14, 9, 13, 0, 357, 348, 17],
"windSpeed": [4, 4, 4, 7, 4, 3, 3, 4, 3, 3, 3, 4, 4, 4, 3],
"humidityPct": [84, 83, 89, 93, 95, 93, 93, 92, 93, 95, 92, 92, 91, 89, 91],
"qualifier": [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null],
"snowRange": ["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
"thunderEnum": [0, 0, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
"thunderEnumPhrase": ["No thunder", "No thunder", "Thunder expected", "Thunder possible", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected", "Thunder expected"]
}
}
}

您可以使用array_column仅获取值。
然后,如果您使用array_combine和array_keys,您也可以获得密钥。

$arr = json_decode($json, true);
$todayvalues = array_column($arr["vt1dailyforecast"]["day"], 0);
$todaykeys = array_keys($arr["vt1dailyforecast"]["day"]);
$today = array_combine($todaykeys, $todayvalues);

输出

array(20) {
["dayPartName"]=>
string(5) "Today"
["precipPct"]=>
int(10)
["precipAmt"]=>
float(0)
["precipType"]=>
string(4) "rain"
["temperature"]=>
int(96)
["uvIndex"]=>
int(10)
["uvDescription"]=>
string(9) "Very High"
["icon"]=>
int(30)
["iconExtended"]=>
int(3000)
["phrase"]=>
string(13) "Partly Cloudy"
["narrative"]=>
string(50) "Partly cloudy. High 96F. Winds light and variable."
["cloudPct"]=>
int(36)
["windDirCompass"]=>
string(1) "S"
["windDirDegrees"]=>
int(177)
["windSpeed"]=>
int(5)
["humidityPct"]=>
int(57)
["qualifier"]=>
NULL
["snowRange"]=>
string(0) ""
["thunderEnum"]=>
int(0)
["thunderEnumPhrase"]=>
string(10) "No thunder"
}

https://3v4l.org/7PXan

最新更新