问题描述
我们通过其API获取来自远程服务器的数据.不幸的是,他们的API不按日期订购返回的数据.
我正在尝试,没有多大成功,弄清楚如何重新组织数据,以便它由next_bookable_date订购.我们正在使用PHP和SimpleXMLElement来解析数据并创建一个字符串,然后将其插入网页.但当前结果与数据出现在返回的XML中相同.
基本XML结果如下.有更多的数据,我剥掉了节省空间.
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 ) ) )
我们正在使用的PHP代码生成HTML字符串(再次剥离了一些HTML代码来保存空间):
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输出?
推荐答案
您可以使用 usort()来排序多维阵列或物体.我写了这一点代码来解释如何使用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看起来有些如此:
<?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 函数以使用e.g.属性确定订单.
问题描述
We are fetching data from a remote server via their API. Unfortunately, their API does not order the returned data by date.
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.
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 ) ) )
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"; }
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?
推荐答案
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; } ?>
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>
If that is not the case, then you need to rewrite the sorttours function to use e.g. attributes to determine the order.