1

I like to sort my XML response.

This is my code:

// Make some cURL

// Create a simple XML element
$xml = new SimpleXMLElement($resp, LIBXML_NOWARNING, false);

// Output
foreach ($xml->Departure as $departure){

    // DEFINE VARIABLES BASED ON XML RESPONSE
    $name = $departure['name'];
    $rtDate = $departure['rtDate'];
    $rtTime = $departure['rtTime'];
    $direction = $departure['direction'];
    $trainCategory = $departure['trainCategory'];

    // CALCULATE DURATION UNTIL NEXT DEPARTURE
    $prognosedTime = new DateTime($rtTime);
    $currentTime = new DateTime($time);
    $interval = $currentTime->diff($prognosedTime);

    // OUTPUT FOR BROWSERS
    echo $interval->format('%i') . ' Min: ' . $name . ' > ' . $direction . '",';
    echo $trainCategory;
    echo "<hr/>";
};
?>

Result:

7 Min: Bus 240 > S Ostbahnhof
Bus
-------------------------------------
8 Min: Tram M10 > S+U Warschauer Str.
MetroTram
-------------------------------------
2 Min: U1 > Uhlandstr.
U-Bahn
-------------------------------------
0 Min: Tram M10 > S+U Hauptbahnhof
MetroTram

Problem:

My result should be sorted by $interval

I read PHP sorting issue with simpleXML several times but I don't get it. So I wanted a shorter solution (for bloody beginners) and found something nice in Sort Foreach Loop after ID. But then I need arrays. Another solution is very close to that and shows how to define arrays: ASC sort foreach. But here I have no idea how to put all my data into an array as I never know how many rows the response will have. I believe I am very close to a solution but don't get it since 2 days. narf

Community
  • 1
  • 1
Jotto
  • 3
  • 6

0 Answers0