0

I'm scratching my head here. I'm a novice at this. I've been forcing myself to find this out by myself and not always relying on your help here. But I've ground my fingers on this one.

What I'm trying to do is to sort an array which I am getting from an external API (Link in code) and everything I try the entire list disappears or doesn't do anything. I've been reading over this documentation over and over and I can't seem to wrap my head around it

I'm trying to sort "realArrival" from value low to high.

Anywho here is my code:

    <?php
    $url = 'http://apis.is/flight?language=en&type=arrivals';
    $json = file_get_contents($url);
    $results = json_decode($json, TRUE);
    echo '<table class="highlight responsive-table purple darken-4">';
        echo "<tr>";
            echo '<th>Date</th>';
            echo '<th>Flight Number</th>';
            echo '<th>Airline</th>';
            echo '<th>From</th>';
            echo '<th>Schedule. Time</th>';
            echo '<th>Status</th>';
        echo "</tr>";
    foreach ($results['results'] as $item => $val) {
        echo "<tr>";
            echo '<td>'.$item = $val['date'].'</td>';
            echo '<td>'.$item = $val['flightNumber'].'</td>';
            echo '<td>'.$item = $val['airline'].'</td>';
            echo '<td>'.$item = $val['from'].'</td>';
            echo '<td>'.$item = $val['plannedArrival'].'</td>';
            echo '<td>'.$item = $val['realArrival'].'</td>';
        echo '</tr>';
    }
    echo '</table>';
    ?>

Thanks in advance!

2 Answers 2

1

I'm not seeing any code that attempts to sort...

That said, you may want to use usort(). You'll need to create an appropriate function that returns -1, 0, or 1 depending on the results of the comparison, and then reference that function in your call to usort().

Sign up to request clarification or add additional context in comments.

Comments

0

You need to sort array first using usort() and then print it inside table:-

 <?php
    $url = 'http://apis.is/flight?language=en&type=arrivals';
    $json = file_get_contents($url);
    $results = json_decode($json, TRUE);
    $results = $results['results'];

    usort($results, 'compare_time');
    function compare_time($a,$b){
       $first_time = strtotime(substr($a['realArrival'], -4));
       $second_time = strtotime(substr($b['realArrival'], -4));

       return ($first_time < $second_time)  ? 1: -1;
    }

    echo '<table class="highlight responsive-table purple darken-4">';
        echo "<tr>";
            echo '<th>Date</th>';
            echo '<th>Flight Number</th>';
            echo '<th>Airline</th>';
            echo '<th>From</th>';
            echo '<th>Schedule. Time</th>';
            echo '<th>Status</th>';
        echo "</tr>";
    foreach ($results as $item => $val) {
        echo "<tr>";
            echo '<td>'.$val['date'].'</td>';
            echo '<td>'.$val['flightNumber'].'</td>';
            echo '<td>'.$val['airline'].'</td>';
            echo '<td>'.$val['from'].'</td>';
            echo '<td>'.$val['plannedArrival'].'</td>';
            echo '<td>'.$val['realArrival'].'</td>';
        echo '</tr>';
    }
    echo '</table>';
?>

5 Comments

This is what I've been looking for. Will study PHP a little more thanks a bunch!
If I may ask. I can change the language of the API from en to is and when it says estimated in is it says "Áætluð koma " and it just goes anywhere. Why is that?
@Hokus_Pokus check my edited solution and test it for different language change.
It worked, I flipped return ($first_time < $second_time) ? -1: 1; Now all I need to do is to figure out move all the empty values to the bottom so it counts the timestamp on the top low to high
@Hokus_Pokus you need to ask a new question. Asking question again and again in comment is not a good idea. Please ask a new question with proper input data and most important your code effort what you tried so far. Address me the link, if i am free i will try to answer it, otherwise you will get answer from some-one for sure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.