0

APIを介してリモートサーバーからデータを取得しています。残念ながら、彼らの API は返されたデータを日付順に並べ替えません。

next_bookable_date で順序付けられるようにデータを再編成する方法を見つけようとしていますが、あまり成功していません。PHP と SimpleXMLElement を使用してデータを解析し、Web ページに挿入される文字列を作成します。ただし、現在の結果は、返された 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_name;
$tourdate = $tour->next_bookable_date;

// create string for dpt-soon
$dpt_soon_list .= "<li> some html using the above values </li>\n";
}

リモート サーバーから受け取った XML データの順序を変更する方法はありますか? または、foreach の実行時に PHP 出力を並べ替える方法はありますか?

4

1 に答える 1

1

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関数を書き直して、たとえば属性を使用して順序を決定する必要があります。

于 2013-03-04T15:27:23.107 に答える