Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于XML数据对XML结果重新排序_Php_Xml_Api_Xml Parsing - Fatal编程技术网

Php 基于XML数据对XML结果重新排序

Php 基于XML数据对XML结果重新排序,php,xml,api,xml-parsing,Php,Xml,Api,Xml Parsing,我们正在通过远程服务器的API获取数据。不幸的是,他们的API没有按日期对返回的数据进行排序 我正在尝试如何重新组织数据,以便在下一个可预订日期之前订购,但没有取得多大成功。我们使用PHP和SimpleXMLElement解析数据并创建一个字符串,然后将其插入网页。但当前结果的顺序与返回的XML中显示的数据的顺序相同 下面是基本的XML结果。还有更多的数据,我把它们剥离出来以节省空间 SimpleXMLElement Object ( [request] => GET search

我们正在通过远程服务器的API获取数据。不幸的是,他们的API没有按日期对返回的数据进行排序

我正在尝试如何重新组织数据,以便在下一个可预订日期之前订购,但没有取得多大成功。我们使用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
        )
    )
)
我们用来生成html字符串的PHP代码(为了节省空间,再次剥离了一些html代码):

foreach($result->tour as$tour){
$tourname=$tour->tour\u name;
$tourdate=$tour->next\u bookable\u date;
//尽快为dpt创建字符串
$dpt_soon_list.=“
  • 使用上述值的一些html
  • \n”; }
    一旦我们从远程服务器接收到XML数据,有没有办法对其重新排序?或者,在运行foreach时,是否有方法对PHP输出进行重新排序?

    您可以使用它对多维数组或对象进行排序。我编写了这段代码来解释如何在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;
    }
    ?>
    
    children();
    $Tours=$Tours[“tour”];
    //在数组上调用usort
    usort($Tours,简称“sorttours”);
    //输出结果
    echo“.print_r($Tours,true)。”;
    //指定一个条目何时大于、等于或小于另一个条目的函数
    功能排序器($a,$b){
    //将字符串解析为日期以进行比较
    $Date1=STROTIME($a->下一个可预订日期);
    $Date2=标准时间($b->下一个可预订日期);
    //如果相等,则返回0
    如果($Date1==$Date2){
    返回0;
    }
    //如果Date1较大,则返回1,否则返回-1
    返回($Date1>$Date2)?1:-1;
    }
    ?>
    
    本例假设XML看起来有点像:

    
    2013-05-13
    泰国之旅
    2013-05-12
    巴厘岛之旅
    2013-05-05
    夏威夷之旅
    2013-05-06
    不丹之旅
    

    如果不是这样,那么您需要重写sorttours函数,以使用例如属性来确定顺序。

    如果您正在执行纯xml->输出,则使用simplexml提取相关数据,将其放入标准PHP数组,然后使用,您还可以使用xslt直接执行xml->html转换相关:;谢谢你给我指明了正确的方向。经过一点尝试和错误,我让它工作了。我遇到的一个问题是生成html字符串的foreach。如果XML只包含一个列表,usort将不会创建数组,foreach将在无限循环中运行。因此,我必须添加一个查看巡更计数的if语句,然后选择将其作为simpleXML处理或使用usort数组路径。我通常建议使用
    iterator\u to\u array
    ,请参见此处:Thank@hakre。直到现在才知道这个函数。今后,我将使用迭代器_to_数组与usort结合使用。一个人永远不会停止学习。
    <?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 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>