根据XML数据对XML结果重新排序 [英] Re-order XML results based on XML data
问题描述
我们正在通过其API从远程服务器获取数据.不幸的是,他们的API并未按日期对返回的数据进行排序.
We are fetching data from a remote server via their API. Unfortunately, their API does not order the returned data by date.
我正在尝试,但没有取得多大的成功,以弄清楚如何重新组织数据,以便在next_bookable_date之前对数据进行排序.我们正在使用PHP和SimpleXMLElement解析数据并创建一个字符串,然后将其插入到网页中.但是,当前结果的顺序与返回的XML中数据的显示顺序相同.
I am trying, without much success, to figure out how to re-organize the data so that it is ordered by the next_bookable_date. We are using PHP and SimpleXMLElement to parse the data and create a string which is then inserted into a webpage. But the current result is in the same order as data appears in the returned XML.
基本XML结果如下.为了节省空间,我删除了更多数据.
The basic XML results are below. There is much more data, that I stripped out to save space.
SimpleXMLElement Object
(
[request] => GET search.xml?start_date=2013-05-03&end_date=2013-05-17
[error] => OK
[total_tour_count] => 4
[tour] => Array
(
[0] => SimpleXMLElement Object
(
[next_bookable_date] => 2013-05-13
[tour_name] => Thailand Tour
)
[1] => SimpleXMLElement Object
(
[next_bookable_date] => 2013-05-12
[tour_name] => Bali Tour
)
[2] => SimpleXMLElement Object
(
[next_bookable_date] => 2013-05-05
[tour_name] => Hawaii Tour
)
[3] => SimpleXMLElement Object
(
[next_bookable_date] => 2013-05-06
[tour_name] => Bhutan Tour
)
)
)
我们用于生成html字符串的PHP代码(再次去除了一些html代码以节省空间):
The PHP code we are using to generate the html string (again stripped of a bit of html code to save space):
foreach($result->tour as $tour) {
$tourname = $tour->tour_name;
$tourdate = $tour->next_bookable_date;
// create string for dpt-soon
$dpt_soon_list .= "<li> some html using the above values </li>\n";
}
一旦我们从远程服务器接收到XML数据,是否可以对XML数据重新排序?还是有一种方法可以在运行foreach时重新排序PHP输出?
Is there a way to re-order the XML data once we receive it from the remote server? Or is there a way to reorder the PHP output when running the foreach?
推荐答案
您可以使用 usort()对多维数组或对象进行排序.我写了这段代码来解释如何与SimpleXML一起使用:
You can use usort() to sort multidimensional arrays or objects. I wrote this bit of code to explain how to use it with SimpleXML:
<?php
// Load the XML file
$xml = simplexml_load_file("xml.xml");
// Get all children into an array
$Tours = (array)$xml->children();
$Tours = $Tours["tour"];
// Call usort on the array
usort($Tours, "sorttours");
// Output results
echo "<pre>".print_r($Tours, true)."</pre>";
// The function that specifies when an entry is larger, equal or smaller than another
function sorttours($a, $b) {
// Parse strings into a date for comparison
$Date1 = strtotime($a->next_bookable_date);
$Date2 = strtotime($b->next_bookable_date);
// If equal, return 0
if ($Date1 == $Date2) {
return 0;
}
// If Date1 is larger, return 1, otherwise -1
return ($Date1 > $Date2) ? 1 : -1;
}
?>
此示例假定XML看起来像这样:
This example assumes that the XML looks somewhat like this:
<?xml version="1.0"?>
<tours>
<tour>
<next_bookable_date>2013-05-13</next_bookable_date>
<tour_name>Thailand Tour</tour_name>
</tour>
<tour>
<next_bookable_date>2013-05-12</next_bookable_date>
<tour_name>Bali Tour</tour_name>
</tour>
<tour>
<next_bookable_date>2013-05-05</next_bookable_date>
<tour_name>Hawaii Tour</tour_name>
</tour>
<tour>
<next_bookable_date>2013-05-06</next_bookable_date>
<tour_name>Bhutan Tour</tour_name>
</tour>
</tours>
如果不是这种情况,则需要重写 sorttours 函数以使用例如属性来确定顺序.
If that is not the case, then you need to rewrite the sorttours function to use e.g. attributes to determine the order.
这篇关于根据XML数据对XML结果重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!