2017-08-02 73 views
0

我试图在过去的24小时内从accuweather API回应某个位置的解码的json数据。输出会是这样的“,在‘城市’昨天的天气 日期:时间 - 温度 日期:时间 - 温度 日期:hour24 - temp24ECHO历史ACCUWEATHER数据JSON

这是一个数据。 JSON format

这是我的代码使用var转储和尝试呼应它在一个更加复杂和整洁的方式,提前

$json = file_get_contents('http://dataservice.accuweather.com/currentconditions/v1/328328/historical/24?apikey=GKu1AhOuGN0aAnNEMyiWgKD5fAmo4dpN'); 

//decode JSON to array 
$data = json_decode($json,true); 

//show data 
print_r($data); 

foreach ($data['list'] as $hour => $value) { 
    echo "The temperature for the hour ". $hours ." was ". $value[Temperature]."<br/>"; 
    # code... 
} 

感谢。

+0

对不起。我不明白。您的文本显示“温度将是...”,但数据显示的是历史数据。你想做什么?您是否尝试输出每个历史项目的时间戳和温度? – Andreas

+0

这是我的语法错误,温度是*我想输出每小时的时间戳和当时的最后24小时的温度 – R1ddler

+0

明白了!将在下面发布答案。 – Andreas

回答

0

该代码将遍历每个时间戳并输出温度的时间戳和度量值。
不确定你想要什么,但如果它不正确,这是一个简单的改变。

<?php 
$json = file_get_contents('http://dataservice.accuweather.com/currentconditions/v1/328328/historical/24?apikey=GKu1AhOuGN0aAnNEMyiWgKD5fAmo4dpN'); 

//decode JSON to array 
$data = json_decode($json,true); 

//show data 
echo "<pre>"; 
// print_r($data); 
echo "</pre>"; 
foreach ($data as $value) { 
    echo "The temperature for the hour ". $value["LocalObservationDateTime"] ." will be ". $value["Temperature"]["Metric"]["Value"]."<br/>"; 
                             // or "Imperial" 
    # code... 
} 

编辑显示日期()函数。

foreach ($data as $value) { 
    echo "The temperature for the hour ". date("Y-m-d H:i", $value["EpochTime"]) ." will be ". $value["Temperature"]["Metric"]["Value"]."<br/>"; 
    # code... 
} 

我用了“H”小时。这将显示格式09:55,如果你想9:55使用“G”。
http://php.net/manual/en/function.date.php

+0

谢谢,这就像一个魅力。现在我看到我的代码中出现了错误。我对JSON数据没有多少经验,是否有办法将LocalObservationDateTime剪切为只输出日期和时间。从这个2017-08-02T11:55:00 + 02:00到这样的事情2017-08-02 11:55:00。 – R1ddler

+0

@ R1ddler是的。但我宁愿使用另一个项目。 'EpochTime'是UNIX观察的时间。如果你在php设置日期()中有你的时区,那么你应该给你一个好的时间戳。将更新我的答案给你看。 – Andreas