php - how sort & display data?

33

I have this code from a weather report

$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)

printing an array

Example output:

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 )

How could I sort this data in a minimal and modern way? maybe something like this: enter image description here

I am quite unexperienced. Any help is appreciated!

200

Answer

Solution:

You can use table something like this. Just example:

    <?php
    $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'],
            ]);
        }

    ?>
    <table border=1 cellpadding=5 class="page_speed_1913855585">
    <?php foreach ($result as $value) { ?>
    <tr><td>Bergen <br /><?php echo $value['from'] ?> to  <?php echo $value['to'] ?></td>
       <td><?php echo $value['symbol'] ?></td>
       <td><?php echo $value['temperature'] ?></td>
       <td><?php echo $value['windDirection'] ?></td>
       <td><?php echo $value['windSpeed'] ?></td></tr>
    <?php } ?>
</table>

People are also looking for solutions to the problem: php - Update a table sales with values from table products with WHERE clause in both Update and select query

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.