Philyteach Posted January 17, 2014 Share Posted January 17, 2014 (edited) Hi everyone, let me first preface this by saying that I know very little about php and json. What I know is that I need a webpage that can provide specific weather data for a group of coxswains using their cellphones while out on patrol. Don't worry that you might be talking down to me, because I promise that you won't be when it comes to my codeing skills. That said, I created an API key for weather underground. This provides me with a json file like this: { "response": { "version":"0.1", "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", "features": { "conditions": 1 , "tide": 1 } } , "current_observation": { "image": { "url":"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png", "title":"Weather Underground", "link":"http://www.wunderground.com" }, "display_location": { "full":"Marcus Hook, PA", "city":"Marcus Hook", "state":"PA", "state_name":"Pennsylvania", "country":"US", "country_iso3166":"US", "zip":"19060", "magic":"3", "wmo":"99999", "latitude":"39.83526230", "longitude":"-75.44973755", "elevation":"28.00000000" }, "observation_location": { "full":"Radnor Green, Claymont, Delaware", "city":"Radnor Green, Claymont", "state":"Delaware", "country":"US", "country_iso3166":"US", "latitude":"39.806858", "longitude":"-75.468452", "elevation":"129 ft" }, "estimated": { }, "station_id":"KDECLAYM2", "observation_time":"Last Updated on January 17, 1:48 PM EST", "observation_time_rfc822":"Fri, 17 Jan 2014 13:48:43 -0500", "observation_epoch":"1389984523", "local_time_rfc822":"Fri, 17 Jan 2014 13:48:43 -0500", "local_epoch":"1389984523", "local_tz_short":"EST", "local_tz_long":"America/New_York", "local_tz_offset":"-0500", "weather":"Scattered Clouds", "temperature_string":"41.7 F (5.4 C)", "temp_f":41.7, "temp_c":5.4, "relative_humidity":"67%", "wind_string":"From the South at 2.0 MPH Gusting to 4.0 MPH", "wind_dir":"South", "wind_degrees":186, "wind_mph":2.0, "wind_gust_mph":"4.0", "wind_kph":3.2, "wind_gust_kph":"6.4", "pressure_mb":"1014", "pressure_in":"29.94", "pressure_trend":"-", "dewpoint_string":"32 F (-0 C)", "dewpoint_f":32, "dewpoint_c":-0, "heat_index_string":"NA", "heat_index_f":"NA", "heat_index_c":"NA", "windchill_string":"42 F (5 C)", "windchill_f":"42", "windchill_c":"5", "feelslike_string":"42 F (5 C)", "feelslike_f":"42", "feelslike_c":"5", "visibility_mi":"10.0", "visibility_km":"16.1", "solarradiation":"--", "UV":"2","precip_1hr_string":"0.00 in ( 0 mm)", "precip_1hr_in":"0.00", "precip_1hr_metric":" 0", "precip_today_string":"0.00 in (0 mm)", "precip_today_in":"0.00", "precip_today_metric":"0", "icon":"partlycloudy", "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif", "forecast_url":"http://www.wunderground.com/US/PA/Marcus_Hook.html", "history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KDECLAYM2", "ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=39.806858,-75.468452" } , "tide": { "tideInfo": [ { "tideSite":"Marcus Hook, Pennsylvania (sub)", "lat":"39.8117", "lon":"-75.4117", "units":"feet", "type":"tide", "tzname":"America/New_York" } ], "tideSummary": [ { "date": { "pretty": "5:03 PM EST on January 17, 2014", "year": "2014", "mon": "01", "mday": "17", "hour": "17", "min": "03", "tzname": "America/New_York", "epoch":"1389996218" }, "utcdate": { "pretty": "10:03 PM GMT on January 17, 2014", "year": "2014", "mon": "01", "mday": "17", "hour": "22", "min": "03", "tzname": "UTC", "epoch":"1389996218" }, "data": { "height":"", "type":"Sunset" } }, { "date": { "pretty": "6:48 PM EST on January 17, 2014", "year": "2014", "mon": "01", "mday": "17", "hour": "18", "min": "48", "tzname": "America/New_York", "epoch":"1390002484" }, "utcdate": { "pretty": "11:48 PM GMT on January 17, 2014", "year": "2014", "mon": "01", "mday": "17", "hour": "23", "min": "48", "tzname": "UTC", "epoch":"1390002484" }, "data": { "height":"", "type":"Moonrise" } }, { "date": { "pretty": "8:09 PM EST on January 17, 2014", "year": "2014", "mon": "01", "mday": "17", "hour": "20", "min": "09", "tzname": "America/New_York", "epoch":"1390007349" }, "utcdate": { "pretty": "1:09 AM GMT on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "01", "min": "09", "tzname": "UTC", "epoch":"1390007349" }, "data": { "height":"-0.39 ft", "type":"Low Tide" } }, { "date": { "pretty": "1:23 AM EST on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "01", "min": "23", "tzname": "America/New_York", "epoch":"1390026202" }, "utcdate": { "pretty": "6:23 AM GMT on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "06", "min": "23", "tzname": "UTC", "epoch":"1390026202" }, "data": { "height":"4.78 ft", "type":"High Tide" } }, { "date": { "pretty": "7:19 AM EST on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "07", "min": "19", "tzname": "America/New_York", "epoch":"1390047590" }, "utcdate": { "pretty": "12:19 PM GMT on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "12", "min": "19", "tzname": "UTC", "epoch":"1390047590" }, "data": { "height":"", "type":"Sunrise" } }, { "date": { "pretty": "8:12 AM EST on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "08", "min": "12", "tzname": "America/New_York", "epoch":"1390050752" }, "utcdate": { "pretty": "1:12 PM GMT on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "13", "min": "12", "tzname": "UTC", "epoch":"1390050752" }, "data": { "height":"-0.39 ft", "type":"Low Tide" } }, { "date": { "pretty": "8:19 AM EST on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "08", "min": "19", "tzname": "America/New_York", "epoch":"1390051164" }, "utcdate": { "pretty": "1:19 PM GMT on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "13", "min": "19", "tzname": "UTC", "epoch":"1390051164" }, "data": { "height":"", "type":"Moonset" } }, { "date": { "pretty": "1:36 PM EST on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "13", "min": "36", "tzname": "America/New_York", "epoch":"1390070194" }, "utcdate": { "pretty": "6:36 PM GMT on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "18", "min": "36", "tzname": "UTC", "epoch":"1390070194" }, "data": { "height":"5.26 ft", "type":"High Tide" } }, { "date": { "pretty": "5:04 PM EST on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "17", "min": "04", "tzname": "America/New_York", "epoch":"1390082683" }, "utcdate": { "pretty": "10:04 PM GMT on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "22", "min": "04", "tzname": "UTC", "epoch":"1390082683" }, "data": { "height":"", "type":"Sunset" } }, { "date": { "pretty": "7:44 PM EST on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "19", "min": "44", "tzname": "America/New_York", "epoch":"1390092273" }, "utcdate": { "pretty": "12:44 AM GMT on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "00", "min": "44", "tzname": "UTC", "epoch":"1390092273" }, "data": { "height":"", "type":"Moonrise" } }, { "date": { "pretty": "8:47 PM EST on January 18, 2014", "year": "2014", "mon": "01", "mday": "18", "hour": "20", "min": "47", "tzname": "America/New_York", "epoch":"1390096073" }, "utcdate": { "pretty": "1:47 AM GMT on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "01", "min": "47", "tzname": "UTC", "epoch":"1390096073" }, "data": { "height":"-0.36 ft", "type":"Low Tide" } }, { "date": { "pretty": "2:02 AM EST on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "02", "min": "02", "tzname": "America/New_York", "epoch":"1390114947" }, "utcdate": { "pretty": "7:02 AM GMT on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "07", "min": "02", "tzname": "UTC", "epoch":"1390114947" }, "data": { "height":"4.76 ft", "type":"High Tide" } }, { "date": { "pretty": "7:19 AM EST on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "07", "min": "19", "tzname": "America/New_York", "epoch":"1390133959" }, "utcdate": { "pretty": "12:19 PM GMT on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "12", "min": "19", "tzname": "UTC", "epoch":"1390133959" }, "data": { "height":"", "type":"Sunrise" } }, { "date": { "pretty": "8:49 AM EST on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "08", "min": "49", "tzname": "America/New_York", "epoch":"1390139365" }, "utcdate": { "pretty": "1:49 PM GMT on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "13", "min": "49", "tzname": "UTC", "epoch":"1390139365" }, "data": { "height":"", "type":"Moonset" } }, { "date": { "pretty": "8:53 AM EST on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "08", "min": "53", "tzname": "America/New_York", "epoch":"1390139590" }, "utcdate": { "pretty": "1:53 PM GMT on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "13", "min": "53", "tzname": "UTC", "epoch":"1390139590" }, "data": { "height":"-0.37 ft", "type":"Low Tide" } }, { "date": { "pretty": "2:14 PM EST on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "14", "min": "14", "tzname": "America/New_York", "epoch":"1390158882" }, "utcdate": { "pretty": "7:14 PM GMT on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "19", "min": "14", "tzname": "UTC", "epoch":"1390158882" }, "data": { "height":"5.18 ft", "type":"High Tide" } }, { "date": { "pretty": "5:05 PM EST on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "17", "min": "05", "tzname": "America/New_York", "epoch":"1390169151" }, "utcdate": { "pretty": "10:05 PM GMT on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "22", "min": "05", "tzname": "UTC", "epoch":"1390169151" }, "data": { "height":"", "type":"Sunset" } }, { "date": { "pretty": "8:41 PM EST on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "20", "min": "41", "tzname": "America/New_York", "epoch":"1390182096" }, "utcdate": { "pretty": "1:41 AM GMT on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "01", "min": "41", "tzname": "UTC", "epoch":"1390182096" }, "data": { "height":"", "type":"Moonrise" } }, { "date": { "pretty": "9:25 PM EST on January 19, 2014", "year": "2014", "mon": "01", "mday": "19", "hour": "21", "min": "25", "tzname": "America/New_York", "epoch":"1390184750" }, "utcdate": { "pretty": "2:25 AM GMT on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "02", "min": "25", "tzname": "UTC", "epoch":"1390184750" }, "data": { "height":"-0.37 ft", "type":"Low Tide" } }, { "date": { "pretty": "2:41 AM EST on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "02", "min": "41", "tzname": "America/New_York", "epoch":"1390203660" }, "utcdate": { "pretty": "7:41 AM GMT on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "07", "min": "41", "tzname": "UTC", "epoch":"1390203660" }, "data": { "height":"4.78 ft", "type":"High Tide" } }, { "date": { "pretty": "7:18 AM EST on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "07", "min": "18", "tzname": "America/New_York", "epoch":"1390220326" }, "utcdate": { "pretty": "12:18 PM GMT on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "12", "min": "18", "tzname": "UTC", "epoch":"1390220326" }, "data": { "height":"", "type":"Sunrise" } }, { "date": { "pretty": "9:18 AM EST on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "09", "min": "18", "tzname": "America/New_York", "epoch":"1390227517" }, "utcdate": { "pretty": "2:18 PM GMT on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "14", "min": "18", "tzname": "UTC", "epoch":"1390227517" }, "data": { "height":"", "type":"Moonset" } }, { "date": { "pretty": "9:34 AM EST on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "09", "min": "34", "tzname": "America/New_York", "epoch":"1390228453" }, "utcdate": { "pretty": "2:34 PM GMT on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "14", "min": "34", "tzname": "UTC", "epoch":"1390228453" }, "data": { "height":"-0.36 ft", "type":"Low Tide" } }, { "date": { "pretty": "2:53 PM EST on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "14", "min": "53", "tzname": "America/New_York", "epoch":"1390247607" }, "utcdate": { "pretty": "7:53 PM GMT on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "19", "min": "53", "tzname": "UTC", "epoch":"1390247607" }, "data": { "height":"5.10 ft", "type":"High Tide" } }, { "date": { "pretty": "5:07 PM EST on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "17", "min": "07", "tzname": "America/New_York", "epoch":"1390255620" }, "utcdate": { "pretty": "10:07 PM GMT on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "22", "min": "07", "tzname": "UTC", "epoch":"1390255620" }, "data": { "height":"", "type":"Sunset" } }, { "date": { "pretty": "9:39 PM EST on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "21", "min": "39", "tzname": "America/New_York", "epoch":"1390271968" }, "utcdate": { "pretty": "2:39 AM GMT on January 21, 2014", "year": "2014", "mon": "01", "mday": "21", "hour": "02", "min": "39", "tzname": "UTC", "epoch":"1390271968" }, "data": { "height":"", "type":"Moonrise" } }, { "date": { "pretty": "10:04 PM EST on January 20, 2014", "year": "2014", "mon": "01", "mday": "20", "hour": "22", "min": "04", "tzname": "America/New_York", "epoch":"1390273452" }, "utcdate": { "pretty": "3:04 AM GMT on January 21, 2014", "year": "2014", "mon": "01", "mday": "21", "hour": "03", "min": "04", "tzname": "UTC", "epoch":"1390273452" }, "data": { "height":"-0.40 ft", "type":"Low Tide" } }, { "date": { "pretty": "3:19 AM EST on January 21, 2014", "year": "2014", "mon": "01", "mday": "21", "hour": "03", "min": "19", "tzname": "America/New_York", "epoch":"1390292379" }, "utcdate": { "pretty": "8:19 AM GMT on January 21, 2014", "year": "2014", "mon": "01", "mday": "21", "hour": "08", "min": "19", "tzname": "UTC", "epoch":"1390292379" }, "data": { "height":"4.83 ft", "type":"High Tide" } }, { "date": { "pretty": "7:18 AM EST on January 21, 2014", "year": "2014", "mon": "01", "mday": "21", "hour": "07", "min": "18", "tzname": "America/New_York", "epoch":"1390306692" }, "utcdate": { "pretty": "12:18 PM GMT on January 21, 2014", "year": "2014", "mon": "01", "mday": "21", "hour": "12", "min": "18", "tzname": "UTC", "epoch":"1390306692" }, "data": { "height":"", "type":"Sunrise" } }, { "date": { "pretty": "9:47 AM EST on January 21, 2014", "year": "2014", "mon": "01", "mday": "21", "hour": "09", "min": "47", "tzname": "America/New_York", "epoch":"1390315667" }, "utcdate": { "pretty": "2:47 PM GMT on January 21, 2014", "year": "2014", "mon": "01", "mday": "21", "hour": "14", "min": "47", "tzname": "UTC", "epoch":"1390315667" }, "data": { "height":"", "type":"Moonset" } }, { "date": { "pretty": "10:18 AM EST on January 21, 2014", "year": "2014", "mon": "01", "mday": "21", "hour": "10", "min": "18", "tzname": "America/New_York", "epoch":"1390317480" }, "utcdate": { "pretty": "3:18 PM GMT on January 21, 2014", "year": "2014", "mon": "01", "mday": "21", "hour": "15", "min": "18", "tzname": "UTC", "epoch":"1390317480" }, "data": { "height":"-0.34 ft", "type":"Low Tide" } } ], "tideSummaryStats": [ { "maxheight":5.260000, "minheight":-0.400000 } ] } } I wrote a php script like this to parse the data that I need and display it: <!DOCTYPE html> <html> <body> <?php $json_string = file_get_contents("http://api.wunderground.com/api/<key removed for this post>/conditions/tide/q/PA/Marcus_hook.json"); $parsed_json = json_decode($json_string); $wlocation = $parsed_json->{'current_observation'}->{'display_location'}->{'full'}; $temp_f = $parsed_json->{'current_observation'}->{'temp_f'}; $humid = $parsed_json->{'current_observation'}->{'relative_humidity'}; $wind = $parsed_json->{'current_observation'}->{'wind_string'}; $pressure = $parsed_json->{'current_observation'}->{'pressure_mb'}; $pressuretrend = $parsed_json->{'current_observation'}->{'pressure_trend'}; $feels = $parsed_json->{'current_observation'}->{'feelslike_string'}; $wtime = $parsed_json->{'current_observation'}->{'observation_time_rfc822'}; $tlocation = $parsed_json->{'tide'}->{'tideinfo'}->{'tidesite'}; /* $temp_f = $parsed_json->{'current_observation'}->{'temp_f'}; $humid = $parsed_json->{'current_observation'}->{'relative_humidity'}; $wind = $parsed_json->{'current_observation'}->{'wind_string'}; $pressure = $parsed_json->{'current_observation'}->{'pressure_mb'}; $pressuretrend = $parsed_json->{'current_observation'}->{'pressure_trend'}; $feels = $parsed_json->{'current_observation'}->{'feelslike_string'}; $wtime = $parsed_json->{'current_observation'}->{'observation_time_rfc822'};*/ echo "Current temperature in ${wlocation} is: ${temp_f} F, but it feels like ${feels}<BR>"; echo "The current humidity is ${humid}<BR>"; echo "The winds are ${wind}<BR>"; if ($pressuretrend == "+") { echo "The current pressure is ${pressure} and rising <BR>"; } else { echo "The current pressure is ${pressure} and falling <BR>"; } echo "This weather report last updated ${wtime} <BR>"; echo "<Hr>"; echo "<BR>"; ?> </body> </html> Which prints out a nice report like this: Current temperature in Marcus Hook, PA is: 41.9 F, but it feels like 42 F (6 C)The current humidity is 68%The winds are From the South at 2.0 MPH Gusting to 2.0 MPHThe current pressure is 1014 and fallingThis weather report last updated Fri, 17 Jan 2014 13:57:08 -0500 What I can't figure out, as in completely and hopelessly lost, is how in the world to get it to make a table of the tides. The json file, for example, uses "pretty" over and over again. I need it to find EACH instance of "pretty" and add it to the tide chart, not just one. I hope I'm clear on what I'm trying to do. If not, please ask questions, just keep in mind that I'm a coding infant! Edited January 17, 2014 by Philyteach Quote Link to comment Share on other sites More sharing options...
requinix Posted January 17, 2014 Share Posted January 17, 2014 First a comment: please don't use the ->{} syntax when it's not necessary. It's valid, yes, but not really something that should be used often, and definitely not when there's a "normal" version that works. $wlocation = $parsed_json->current_observation->display_location->full;It's a bit hard to tell from that JSON (it's formatted oddly) but I think the array you want to loop over is tide->tide_summary. foreach ($parsed_json->tide->tide_summary as $summary) { print_r($summary); }To get a table... it depends how you want it to be outputted. What should the resultant HTML look like? Maybe with some sample data? Quote Link to comment Share on other sites More sharing options...
Philyteach Posted January 17, 2014 Author Share Posted January 17, 2014 Requinix, Thank you for your help. I apologize that my syntax is goofy. I'm reading php books and adapting examples from the book and internet to do what I need. I'm completely open to suggestions on how to do the code simpler/cleaner. I just personally don't have the skills for it. This is just one of those situations were "while I suck at it, everyone else I know sucks worse, but it needs to be done" if you know what I mean! The whole point of this page is a clean simple text display of mission critical data that can be pulled up and displayed quickly on a mobile phone using 3g. For the tide data I'm simply looking for table that displays date time type height 1/17/14 6:18am high +4.72 feet 1/17/14 12:15pm slack +1.2 feet 1/17/14 5:45 pm low -3.5 feet just very simple and low bandwidth. Thanks again for any help/suggestions. Quote Link to comment Share on other sites More sharing options...
Philyteach Posted January 17, 2014 Author Share Posted January 17, 2014 http://tbone.biol.sc.edu/tide/tideshow.cgi?site=Marcus+Hook%2C+Pennsylvania&glen=2&units=f&type=mobile is exactly what I'm looking for! Marcus Hook, Pennsylvania 17 January 2014 - 21 January 201439.8117° N, 75.4100° W2014-01-17 Fri 00:36 EST 4.79 feet High Tide2014-01-17 Fri 07:19 EST -0.30 feet Low Tide2014-01-17 Fri 07:20 EST Sunrise2014-01-17 Fri 07:47 EST Moonset2014-01-17 Fri 12:48 EST 5.26 feet High Tide2014-01-17 Fri 17:03 EST Sunset2014-01-17 Fri 18:48 EST Moonrise2014-01-17 Fri 19:58 EST -0.32 feet Low Tide2014-01-18 Sat 01:14 EST 4.71 feet High Tide2014-01-18 Sat 07:19 EST Sunrise2014-01-18 Sat 07:57 EST -0.28 feet Low Tide2014-01-18 Sat 08:19 EST Moonset2014-01-18 Sat 13:23 EST 5.16 feet High Tide2014-01-18 Sat 17:04 EST Sunset2014-01-18 Sat 19:44 EST Moonrise2014-01-18 Sat 20:36 EST -0.28 feet Low Tide2014-01-19 Sun 01:49 EST 4.66 feet High Tide2014-01-19 Sun 07:19 EST Sunrise2014-01-19 Sun 08:37 EST -0.30 feet Low Tide2014-01-19 Sun 08:49 EST Moonset2014-01-19 Sun 13:58 EST 5.05 feet High Tide2014-01-19 Sun 17:05 EST Sunset2014-01-19 Sun 20:41 EST Moonrise2014-01-19 Sun 21:14 EST -0.28 feet Low Tide2014-01-20 Mon 02:23 EST 4.66 feet High Tide2014-01-20 Mon 07:18 EST Sunrise2014-01-20 Mon 09:18 EST Moonset2014-01-20 Mon 09:19 EST -0.33 feet Low Tide2014-01-20 Mon 14:32 EST 4.92 feet High Tide2014-01-20 Mon 17:06 EST Sunset2014-01-20 Mon 21:39 EST Moonrise2014-01-20 Mon 21:53 EST -0.32 feet Low Tide Quote Link to comment Share on other sites More sharing options...
Barand Posted January 18, 2014 Share Posted January 18, 2014 try something like $data = json_decode($str, true); echo "<pre><p>{$data['tide']['tideInfo'][0]['tideSite']}</p>"; $units = $data['tide']['tideInfo'][0]['units']; foreach ($data['tide']['tideSummary'] as $tidedata){ echo $tidedata['date']['pretty'] . ' ' ; if ($tidedata['data']['height']) { echo floatval($tidedata['data']['height']) . " $units "; } echo $tidedata['data']['type'] . '<br/>' ; } echo '</pre>; Quote Link to comment Share on other sites More sharing options...
Solution boompa Posted January 18, 2014 Solution Share Posted January 18, 2014 Another option: $json = json_decode($f); $zone = new DateTimeZone($json->tide->tideInfo[0]->tzname); $unit = $json->tide->tideInfo[0]->units; echo ("<table>\n"); echo ("<tr><th>Date/Time</th><th>Height</th><th>Type</th></tr>\n"); // Iterate through each tide->tideSummary object in the array foreach($json->tide->tideSummary as $tide) { // Create the DateTime object $dt = DateTime::createFromFormat('U', $tide->utcdate->epoch); // Must specifically set TZ, else UTC is used $dt->setTimeZone($zone); // Echo out the table row echo("<tr>\n"); // Format the DateTime as desired // See http://us1.php.net/manual/en/datetime.format.php echo("<td>{$dt->format('Y-m-d H:i:s e')}</td>\n"); // Use printf to extract just the numeric part of the height, // then add the unit (which is the full spelling, rather than // the abbreviation provided within the height element). // This is a fault of the API; if it is going to provide the // unit as it is, it should now be adding that to it (or it // should be providing a raw numeric value) if (!empty($tide->data->height)) { printf("<td>%0.2f %s</td>\n", $tide->data->height, $unit); } else { // Just print an empty cell echo("<td> </td>\n"); } echo("<td>{$tide->data->type}</td>\n"); echo("</tr>\n"); } echo("</table>\n"); Quote Link to comment Share on other sites More sharing options...
Philyteach Posted January 18, 2014 Author Share Posted January 18, 2014 Thank you everyone for all the help! I've been working all night long. With your help the page is starting to look like something! I'm sure you guys could have done my 7-8 hours of work in 1o minutes or so, but from just having picked up the books and starting on the script last week I'm kind of proud of myself. I was even able to modify the examples above to parse to forecast data that I had previously given up on. Thank you again. If you're curious your help can be seen at http://patrolinfo.com/test/index3.php Quote Link to comment 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.