我必须创建一个自动天气,包括雨、雪、云、雾和晴天。
根据季节,我需要为所有天气设置一个百分比:预测将在一天内更新 3 或 4 次。
示例:冬季 | 雨:30% 雪:30% 晴天:10% 阴天:10%,雾:20%
我不知道如何实现基于百分比的随机条件。一些帮助?
非常感谢和抱歉我的英语不好。
好吧,您可以使用:
$location = 'Rome';
$document = file_get_contents(str_replace(" ", "+", "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=".$location));
$xml = new SimpleXMLElement($document);
echo "$location: ".$xml->temp_c."° C";
只需查看 XML 并查看您有哪些可用数据。
编辑
我不明白 OP 第一次想要什么。基本上,它更容易。
$weather = mt_rand(0,100);
$season = 'winter';
switch($season) {
case 'winter': {
if ($weather < 30) {
$output = 'Rainy';
} else if ($weather >=30 && $weather < 60) {
$output = 'Snowy';
}
// goes on on the same ideea of testing the value of $weather
break;
}
// other seasons
}
echo $output;
我建议的艰难是将您的值保存在数组中(例如季节)以及具有一种或另一种天气的机会值。
array (
[winter] => array (
[30] => 'Rainy',
[60] => 'Snowy',
... // the other chances of weather
),
[spring] => array (
...
),
... // and so on
)
用于mt_rand(0,100)
获取随机值和上面的数组来确定天气。
请让我知道这是否适合您。
克劳迪乌的回答很好, 但如果你想用华氏度(F)查看下面的可能示例:
<?php
$location = 'Washington';
$document = file_get_contents(str_replace(" ", "+", "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=" . $location));
$xml = new SimpleXMLElement($document);
echo $xml->temp_f . "° F";
?>