AJM2 Posted January 20, 2022 Share Posted January 20, 2022 I currently have a weather station and website that is shareware software. I get the weather data from Wunderground and inserts into the mysql database to power the site. I would like to use the weather station api and insert directly into the database, essentially by-passing Wunderground. I am able to read the api and see the array but unable to loop over the array and get the data I would like. The data I really want is in the lastData key/array This is the return from the api: array(1) { [0]=> array(3) { ["macAddress"]=> string(17) "FA:F5:C2:98:6F:29" ["lastData"]=> array(27) { ["dateutc"]=> int(1642622880000) ["tempinf"]=> float(77.9) ["humidityin"]=> int(38) ["baromrelin"]=> float(29.956) ["baromabsin"]=> float(29.132) ["tempf"]=> float(77.2) ["battout"]=> int(1) ["humidity"]=> int(47) ["winddir"]=> int(247) ["windspeedmph"]=> float(5.6) ["windgustmph"]=> float(8.1) ["maxdailygust"]=> float(15.9) ["hourlyrainin"]=> int(0) ["eventrainin"]=> int(0) ["dailyrainin"]=> int(0) ["weeklyrainin"]=> int(0) ["monthlyrainin"]=> float(0.52) ["totalrainin"]=> float(48.862) ["solarradiation"]=> float(196.47) ["uv"]=> int(1) ["feelsLike"]=> float(76.83) ["dewPoint"]=> float(55.39) ["feelsLikein"]=> float(77.2) ["dewPointin"]=> float(50.2) ["lastRain"]=> string(24) "2022-01-12T02:50:00.000Z" ["tz"]=> string(15) "America/Chicago" ["date"]=> string(24) "2022-01-19T20:08:00.000Z" } ["info"]=> array(2) { ["name"]=> string(18) "My Weather Station" ["coords"]=> array(5) { ["coords"]=> array(2) { ["lon"]=> float(-197.65635809999999) ["lat"]=> float(38.6587316) } ["address"]=> string(44) "100 Main Street, Anytown, FL 08226, USA" ["location"]=> string(10) "Anytown" ["elevation"]=> float(214.7066497802734) ["geo"]=> array(2) { ["type"]=> string(5) "Point" ["coordinates"]=> array(2) { [0]=> float(-197.65635809999999) [1]=> float(38.6587316) } } } } } } This is the code I have: <?php // Read JSON file $readjson = file_get_contents("https://api.") ; //Decode JSON $data = json_decode($readjson, true); //echo '<pre>' ; var_dump($data); ?> <h1>Weather Forecast for</h1> <table> <tr><th>Date</th><th>Max Temp</th><th>Min Temp</th><th>Precip</th><th>Wspd</th><th>Wgust</th><th>Cloud cover</th></tr> <?php foreach ($data as $key => $key_value) { echo "key is: " .$key. ", "."value is: " .$key_value; echo "<br>"; ?> <tr><td><?php echo $key_value; ?></td></tr> What I get for output: Quote Link to comment https://forums.phpfreaks.com/topic/314440-using-php-read-from-weather-api-and-insert-into-mysql-database/ Share on other sites More sharing options...
Barand Posted January 20, 2022 Share Posted January 20, 2022 Can you post the data returned by the api without all the var_dump junk in it? Can you post your code in a reasonably formatted manner? Quote Link to comment https://forums.phpfreaks.com/topic/314440-using-php-read-from-weather-api-and-insert-into-mysql-database/#findComment-1593489 Share on other sites More sharing options...
gizmola Posted January 21, 2022 Share Posted January 21, 2022 $data is an array with one element that is an array. That array has a lot of elements including the one you say you want. So try: <?php foreach ($data[0]['lastData'] as $key => $key_value) { echo "key: $key, value: $key_value <br>"; } Once you have figured out which keys you actually want, you should be able to access them more directly. So for example: echo "<p>Temperature (F): {$data[0]['lastData']['tempinf']}</p>"; echo "<p>Wind (mph): {$data[0]['lastData']['windspeedmph']}</p>"; I don't see a min/max temp value in the data you provided. Aside from 'lastData', there are several other keys and nested arrays in some cases. For figuring this out in the future, aside from var_dump, print_r is often a useful development/debugging function that can be used as an alternative. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314440-using-php-read-from-weather-api-and-insert-into-mysql-database/#findComment-1593503 Share on other sites More sharing options...
Solution AJM2 Posted January 21, 2022 Author Solution Share Posted January 21, 2022 5 hours ago, gizmola said: $data is an array with one element that is an array. That array has a lot of elements including the one you say you want. So try: <?php foreach ($data[0]['lastData'] as $key => $key_value) { echo "key: $key, value: $key_value <br>"; } Once you have figured out which keys you actually want, you should be able to access them more directly. So for example: echo "<p>Temperature (F): {$data[0]['lastData']['tempinf']}</p>"; echo "<p>Wind (mph): {$data[0]['lastData']['windspeedmph']}</p>"; I don't see a min/max temp value in the data you provided. Aside from 'lastData', there are several other keys and nested arrays in some cases. For figuring this out in the future, aside from var_dump, print_r is often a useful development/debugging function that can be used as an alternative. I did try the print_r function I thought they were the same. I tried your suggestion and it worked! Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/314440-using-php-read-from-weather-api-and-insert-into-mysql-database/#findComment-1593510 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.