0

I've successfully retrieved weather data from OpenWeathMap.org in JSON format, and have decoded it and converted it to an array. Now I'm having trouble displaying the data on the screen in PHP - can someone point out exactly where I'm going wrong. Here is my sample code:

$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'];
  • Note that I've removed my API Key for security reasons.

When I run print_r($weatherData) I get the following output (Note this is just the first few lines and it's been pre-formatted)

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
                )

Why is $weatherData['list']['0']['dt']; not working? Am I missing something?

Williamz902
  • 129
  • 5
  • Did you try to go step by step : `print_r($weatherData['list'])`, then `print_r($weatherData['list'][0])`, then `print_r($weatherData['list'][0]['dt'])`, To see which key fail? – Syscall Mar 09 '18 at 16:10
  • When I initially tried that, I was getting a syntax error, [ unexpected. However, it seems to be working now for some reason! – Williamz902 Mar 09 '18 at 16:26

0 Answers0