从多维数组访问JSON数据

时间:2013-05-22 21:11:03

标签: php json multidimensional-array

我在从wunderground api中提取潮汐数据时遇到了一个小问题。当我使用下面的代码拉出退潮的时间时,我得到一天的准确答案,但第二天的答案是错误的:

$result =  curl_exec($ch); // Getting jSON result string

$parsed_json = json_decode($result);

$low_tide_time = $parsed_json->tide->tideSummary[8]->date->pretty;
echo $low_tide_time;

原因很清楚。数组键(在这种情况下为[8])并非每天一致地分配给相同的潮汐数据类型(退潮)。所以今天[8]可能是与退潮时间相关的数组键,但是明天[8]将是分配给日出,月出等的数组键。

有没有办法根据潮汐类型(退潮)的名称而不是数组键来持续拉动时间?

谢谢!

1 个答案:

答案 0 :(得分:0)

  
    

注意:我不知道Weather Underground的API,我只是关注您在问题和评论中提供的信息。

  

Here's a solution打印数据集上第一个低潮出现的pretty时间字段值:

<?php
function printFirstLowTidePrettyTime($data) {
    foreach ($data['tide']['tideSummary'] as $tideSummary) {
        if ('Low Tide' == $tideSummary['data']['type']) {
            echo "{$tideSummary['date']['pretty']}\n";
            break;
        }
    }
}

$json = <<<JSON
{"response":{"version":"0.1","termsofService":"http://www.wunderground.com/weather/api/d/terms.html","features":{"tide":1}},"tide":{"tideInfo":[{"tideSite":"West Wildwood, Grassy Sound, Hereford Inlet, New Jersey","lat":"39.005","lon":"-74.8267","units":"feet","type":"tide","tzname":"America/New_York"}],"tideSummary":[{"date":{"pretty":"6:57 PM EDT on May 22, 2013","year":"2013","mon":"05","mday":"22","hour":"18","min":"57","tzname":"America/New_York","epoch":"1369263467"},"utcdate":{"pretty":"10:57 PM GMT on May 22, 2013","year":"2013","mon":"05","mday":"22","hour":"22","min":"57","tzname":"UTC","epoch":"1369263467"},"data":{"height":"5.16 ft","type":"High Tide"}},{"date":{"pretty":"8:11 PM EDT on May 22, 2013","year":"2013","mon":"05","mday":"22","hour":"20","min":"11","tzname":"America/New_York","epoch":"1369267887"},"utcdate":{"pretty":"12:11 AM GMT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"00","min":"11","tzname":"UTC","epoch":"1369267887"},"data":{"height":"","type":"Sunset"}},{"date":{"pretty":"1:31 AM EDT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"01","min":"31","tzname":"America/New_York","epoch":"1369287109"},"utcdate":{"pretty":"5:31 AM GMT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"05","min":"31","tzname":"UTC","epoch":"1369287109"},"data":{"height":"-0.12 ft","type":"Low Tide"}},{"date":{"pretty":"4:21 AM EDT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"04","min":"21","tzname":"America/New_York","epoch":"1369297315"},"utcdate":{"pretty":"8:21 AM GMT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"08","min":"21","tzname":"UTC","epoch":"1369297315"},"data":{"height":"","type":"Moonset"}},{"date":{"pretty":"5:40 AM EDT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"05","min":"40","tzname":"America/New_York","epoch":"1369302010"},"utcdate":{"pretty":"9:40 AM GMT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"09","min":"40","tzname":"UTC","epoch":"1369302010"},"data":{"height":"","type":"Sunrise"}},{"date":{"pretty":"7:15 AM EDT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"07","min":"15","tzname":"America/New_York","epoch":"1369307756"},"utcdate":{"pretty":"11:15 AM GMT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"11","min":"15","tzname":"UTC","epoch":"1369307756"},"data":{"height":"4.28 ft","type":"High Tide"}},{"date":{"pretty":"1:33 PM EDT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"13","min":"33","tzname":"America/New_York","epoch":"1369330420"},"utcdate":{"pretty":"5:33 PM GMT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"17","min":"33","tzname":"UTC","epoch":"1369330420"},"data":{"height":"-0.32 ft","type":"Low Tide"}},{"date":{"pretty":"6:42 PM EDT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"18","min":"42","tzname":"America/New_York","epoch":"1369348930"},"utcdate":{"pretty":"10:42 PM GMT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"22","min":"42","tzname":"UTC","epoch":"1369348930"},"data":{"height":"","type":"Moonrise"}},{"date":{"pretty":"7:47 PM EDT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"19","min":"47","tzname":"America/New_York","epoch":"1369352825"},"utcdate":{"pretty":"11:47 PM GMT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"23","min":"47","tzname":"UTC","epoch":"1369352825"},"data":{"height":"5.53 ft","type":"High Tide"}},{"date":{"pretty":"8:12 PM EDT on May 23, 2013","year":"2013","mon":"05","mday":"23","hour":"20","min":"12","tzname":"America/New_York","epoch":"1369354346"},"utcdate":{"pretty":"12:12 AM GMT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"00","min":"12","tzname":"UTC","epoch":"1369354346"},"data":{"height":"","type":"Sunset"}},{"date":{"pretty":"2:25 AM EDT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"02","min":"25","tzname":"America/New_York","epoch":"1369376706"},"utcdate":{"pretty":"6:25 AM GMT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"06","min":"25","tzname":"UTC","epoch":"1369376706"},"data":{"height":"-0.41 ft","type":"Low Tide"}},{"date":{"pretty":"5:08 AM EDT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"05","min":"08","tzname":"America/New_York","epoch":"1369386516"},"utcdate":{"pretty":"9:08 AM GMT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"09","min":"08","tzname":"UTC","epoch":"1369386516"},"data":{"height":"","type":"Moonset"}},{"date":{"pretty":"5:39 AM EDT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"05","min":"39","tzname":"America/New_York","epoch":"1369388371"},"utcdate":{"pretty":"9:39 AM GMT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"09","min":"39","tzname":"UTC","epoch":"1369388371"},"data":{"height":"","type":"Sunrise"}},{"date":{"pretty":"8:08 AM EDT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"08","min":"08","tzname":"America/New_York","epoch":"1369397333"},"utcdate":{"pretty":"12:08 PM GMT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"12","min":"08","tzname":"UTC","epoch":"1369397333"},"data":{"height":"4.40 ft","type":"High Tide"}},{"date":{"pretty":"2:24 PM EDT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"14","min":"24","tzname":"America/New_York","epoch":"1369419866"},"utcdate":{"pretty":"6:24 PM GMT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"18","min":"24","tzname":"UTC","epoch":"1369419866"},"data":{"height":"-0.48 ft","type":"Low Tide"}},{"date":{"pretty":"7:52 PM EDT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"19","min":"52","tzname":"America/New_York","epoch":"1369439546"},"utcdate":{"pretty":"11:52 PM GMT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"23","min":"52","tzname":"UTC","epoch":"1369439546"},"data":{"height":"","type":"Moonrise"}},{"date":{"pretty":"8:13 PM EDT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"20","min":"13","tzname":"America/New_York","epoch":"1369440795"},"utcdate":{"pretty":"12:13 AM GMT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"00","min":"13","tzname":"UTC","epoch":"1369440795"},"data":{"height":"","type":"Sunset"}},{"date":{"pretty":"8:37 PM EDT on May 24, 2013","year":"2013","mon":"05","mday":"24","hour":"20","min":"37","tzname":"America/New_York","epoch":"1369442225"},"utcdate":{"pretty":"12:37 AM GMT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"00","min":"37","tzname":"UTC","epoch":"1369442225"},"data":{"height":"5.80 ft","type":"High Tide"}},{"date":{"pretty":"12:27 AM EDT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"00","min":"27","tzname":"America/New_York","epoch":"1369456023"},"utcdate":{"pretty":"4:27 AM GMT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"04","min":"27","tzname":"UTC","epoch":"1369456023"},"data":{"height":"","type":"Full Moon"}},{"date":{"pretty":"3:17 AM EDT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"03","min":"17","tzname":"America/New_York","epoch":"1369466253"},"utcdate":{"pretty":"7:17 AM GMT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"07","min":"17","tzname":"UTC","epoch":"1369466253"},"data":{"height":"-0.61 ft","type":"Low Tide"}},{"date":{"pretty":"5:38 AM EDT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"05","min":"38","tzname":"America/New_York","epoch":"1369474734"},"utcdate":{"pretty":"9:38 AM GMT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"09","min":"38","tzname":"UTC","epoch":"1369474734"},"data":{"height":"","type":"Sunrise"}},{"date":{"pretty":"6:02 AM EDT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"06","min":"02","tzname":"America/New_York","epoch":"1369476171"},"utcdate":{"pretty":"10:02 AM GMT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"10","min":"02","tzname":"UTC","epoch":"1369476171"},"data":{"height":"","type":"Moonset"}},{"date":{"pretty":"9:01 AM EDT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"09","min":"01","tzname":"America/New_York","epoch":"1369486905"},"utcdate":{"pretty":"1:01 PM GMT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"13","min":"01","tzname":"UTC","epoch":"1369486905"},"data":{"height":"4.47 ft","type":"High Tide"}},{"date":{"pretty":"3:16 PM EDT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"15","min":"16","tzname":"America/New_York","epoch":"1369509366"},"utcdate":{"pretty":"7:16 PM GMT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"19","min":"16","tzname":"UTC","epoch":"1369509366"},"data":{"height":"-0.56 ft","type":"Low Tide"}},{"date":{"pretty":"8:14 PM EDT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"20","min":"14","tzname":"America/New_York","epoch":"1369527243"},"utcdate":{"pretty":"12:14 AM GMT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"00","min":"14","tzname":"UTC","epoch":"1369527243"},"data":{"height":"","type":"Sunset"}},{"date":{"pretty":"8:59 PM EDT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"20","min":"59","tzname":"America/New_York","epoch":"1369529969"},"utcdate":{"pretty":"12:59 AM GMT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"00","min":"59","tzname":"UTC","epoch":"1369529969"},"data":{"height":"","type":"Moonrise"}},{"date":{"pretty":"9:27 PM EDT on May 25, 2013","year":"2013","mon":"05","mday":"25","hour":"21","min":"27","tzname":"America/New_York","epoch":"1369531656"},"utcdate":{"pretty":"1:27 AM GMT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"01","min":"27","tzname":"UTC","epoch":"1369531656"},"data":{"height":"5.94 ft","type":"High Tide"}},{"date":{"pretty":"4:10 AM EDT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"04","min":"10","tzname":"America/New_York","epoch":"1369555804"},"utcdate":{"pretty":"8:10 AM GMT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"08","min":"10","tzname":"UTC","epoch":"1369555804"},"data":{"height":"-0.71 ft","type":"Low Tide"}},{"date":{"pretty":"5:38 AM EDT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"05","min":"38","tzname":"America/New_York","epoch":"1369561099"},"utcdate":{"pretty":"9:38 AM GMT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"09","min":"38","tzname":"UTC","epoch":"1369561099"},"data":{"height":"","type":"Sunrise"}},{"date":{"pretty":"7:04 AM EDT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"07","min":"04","tzname":"America/New_York","epoch":"1369566269"},"utcdate":{"pretty":"11:04 AM GMT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"11","min":"04","tzname":"UTC","epoch":"1369566269"},"data":{"height":"","type":"Moonset"}},{"date":{"pretty":"9:55 AM EDT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"09","min":"55","tzname":"America/New_York","epoch":"1369576511"},"utcdate":{"pretty":"1:55 PM GMT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"13","min":"55","tzname":"UTC","epoch":"1369576511"},"data":{"height":"4.49 ft","type":"High Tide"}},{"date":{"pretty":"4:09 PM EDT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"16","min":"09","tzname":"America/New_York","epoch":"1369598945"},"utcdate":{"pretty":"8:09 PM GMT on May 26, 2013","year":"2013","mon":"05","mday":"26","hour":"20","min":"09","tzname":"UTC","epoch":"1369598945"},"data":{"height":"-0.54 ft","type":"Low Tide"}}],"tideSummaryStats":[{"maxheight":5.94,"minheight":-0.71}]}}
JSON;
$data = json_decode($json, true);

printFirstLowTidePrettyTime($data);

输出:

1:31 AM EDT on May 23, 2013