从Array中提取OpenWeatherMap数据

时间:2018-03-09 16:02:38

标签: php json

我已经以JSON格式从OpenWeathMap.org成功检索了天气数据,并对其进行了解码并将其转换为数组。现在我在用PHP显示屏幕上的数据时遇到了麻烦 - 有人可以指出我出错的地方。这是我的示例代码:

$json = file_get_contents("http://api.openweathermap.org/data/2.5/forecast?id=1040652&units=metric&APPID=MYAPIKEY");
$weatherData = json_decode($json, true);

Echo "Timestamp: " . $weatherData['list']['0']['dt'];
  • 请注意,出于安全原因,我删除了我的API密钥。

当我运行print_r($weatherData)时,我得到以下输出(请注意,这只是前几行并且已预先格式化)

Array
(
    [cod] => 200
    [message] => 0.003
    [cnt] => 40
    [list] => Array
        (
            [0] => Array
                (
                    [dt] => 1520596800
                    [main] => Array
                        (
                            [temp] => 304.79
                            [temp_min] => 301.586
                            [temp_max] => 304.79
                            [pressure] => 1015.06
                            [sea_level] => 1023.63
                            [grnd_level] => 1015.06
                            [humidity] => 57
                            [temp_kf] => 3.21
                        )

                    [weather] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 800
                                    [main] => Clear
                                    [description] => clear sky
                                    [icon] => 01d
                                )

                        )

                    [clouds] => Array
                        (
                            [all] => 0
                        )

                    [wind] => Array
                        (
                            [speed] => 6.57
                            [deg] => 160.006
                        )

                    [rain] => Array
                        (
                        )

                    [sys] => Array
                        (
                            [pod] => d
                        )

                    [dt_txt] => 2018-03-09 12:00:00
                )

为什么$weatherData['list']['0']['dt'];无效?我错过了什么吗?

0 个答案:

没有答案