Re-order XML results based on XML data [duplicate]

你。 提交于 2019-12-11 17:51:26

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15204305/re-order-xml-results-based-on-xml-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!