我喜欢对XML响应进行排序。
这是我的代码:
// 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/>";
};
?>
结果:
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
问题:
我的结果应按$interval
我看了 PHP sorting issue with simpleXML几次,但我没有得到它。所以我想要一个更短的解决方案(对于血腥的初学者)并在Sort Foreach Loop after ID找到了一些不错的东西。但后来我需要数组。另一个解决方案非常接近,并展示了如何定义数组:ASC sort foreach。但在这里我不知道如何将我的所有数据放入数组,因为我从来不知道响应会有多少行。我相信我非常接近解决方案但是从2天开始就没有得到它。 NARF