猿问

如何排序和显示数据?

我从天气预报中获取此代码


$url = ('https://www.yr.no/sted/Norge/Vestland/Bergen/Bergen/varsel.xml');


    $feed = simplexml_load_file($url) or die('Can not connect to server');

    $result = array();

    foreach ($feed->forecast->tabular->time as $content) {

        array_push($result, [ "from" => (string)$content['from'],

            "to" => (string)$content['to'],

            'symbol' => (string)$content->symbol['name'],

            'temperature' => (string)$content->temperature['value'],

            'windDirection' => (string)$content->windDirection['code'],

            'windSpeed' => (string)$content->windSpeed['mps'],

        ]);

    }

print_r($result)

打印阵列


输出示例:


Array ( [0] => Array ( [from] => 2020-02-02T21:00:00 [to] => 2020-02-03T00:00:00 [symbol] => Lettskyet [temperature] => 2 [windDirection] => ENE [windSpeed] => 2.0 ) [1] => Array ( [from] => 2020-02-03T00:00:00 [to] => 2020-02-03T06:00:00 [symbol] => Skyet [temperature] => 1 [windDirection] => E [windSpeed] => 2.1 ) [2] => Array ( [from] => 2020-02-03T06:00:00 [to] => 2020-02-03T12:00:00 [symbol] => Skyet [temperature] => 1 [windDirection] => E [windSpeed] => 2.4 )

如何以最小和现代的方式对这些数据进行排序?也许是这样的:

杨魅力
浏览 113回答 1
1回答

哆啦的时光机

您可以使用类似如下的表。仅举一例:&nbsp; &nbsp; <?php&nbsp; &nbsp; $url = ('https://www.yr.no/sted/Norge/Vestland/Bergen/Bergen/varsel.xml');&nbsp; &nbsp; &nbsp; &nbsp; $feed = simplexml_load_file($url) or die('Can not connect to server');&nbsp; &nbsp; &nbsp; &nbsp; $result = array();&nbsp; &nbsp; &nbsp; &nbsp; foreach ($feed->forecast->tabular->time as $content) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($result, [ "from" => (string)$content['from'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "to" => (string)$content['to'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'symbol' => (string)$content->symbol['name'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'temperature' => (string)$content->temperature['value'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'windDirection' => (string)$content->windDirection['code'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'windSpeed' => (string)$content->windSpeed['mps'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp; &nbsp; <table border=1 cellpadding=5 style=" background:blue; color: #fff;">&nbsp; &nbsp; <?php foreach ($result as $value) { ?>&nbsp; &nbsp; <tr><td>Bergen <br /><?php echo $value['from'] ?> to&nbsp; <?php echo $value['to'] ?></td>&nbsp; &nbsp; &nbsp; &nbsp;<td><?php echo $value['symbol'] ?></td>&nbsp; &nbsp; &nbsp; &nbsp;<td><?php echo $value['temperature'] ?></td>&nbsp; &nbsp; &nbsp; &nbsp;<td><?php echo $value['windDirection'] ?></td>&nbsp; &nbsp; &nbsp; &nbsp;<td><?php echo $value['windSpeed'] ?></td></tr>&nbsp; &nbsp; <?php } ?></table>
随时随地看视频慕课网APP
我要回答