1

雨、雪、雲、霧、晴れなどの自動気象を作成する必要があります。

季節に応じて、すべての天気のパーセンテージを設定する必要があります。予測は1日に3〜4回更新されます。

例:冬| 雨:30%雪:30%晴れ:10%曇り:10%、霧:20%

パーセンテージに基づいてランダムな条件を実装する方法がわかりません。手助け?

私の悪い英語に感謝し、申し訳ありません。

4

2 に答える 2

2

さて、あなたは使うことができます:

$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)してランダムな値を取得し、上記の配列を使用して天気を判断します。

これでうまくいくかどうか教えてください。

于 2010-10-23T13:54:05.943 に答える
2

Claudiuによるすばらしい回答 ですが、華氏(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 . "&deg; F";
 ?>
于 2015-10-15T09:22:48.110 に答える