2016-07-30 55 views
0

我需要對從遠程服務器訪問並由simpleXML訪問的XML文件進行排序(它是從提供者訪問規範中的文件的批准方式 - 所以不能改變)按字段排序遠程XML文件

$propertylist = simplexml_load_file("http://link.to/file.xml?accesskey"); 

我需要梳理上$propertylist->price前高後低,不通過管道將XML內容到一個單獨的文件

我已經看到了(並試圖)在這裏找到,沒有了若干建議成功:(

arsort($propertylist->price);打破代碼

這裏是XML的摘錄:

<?xml version="1.0" encoding="UTF-8"?> 
<properties> 
    ....... 
    <property> 
     <propertyID /> 
     <branchID>1</branchID> 
     <clientName>y</clientName> 
     <branchName>z</branchName> 
     <department>S</department> 
     <referenceNumber>1</referenceNumber> 
     <price>219950</price> 
     <fullDescription><![CDATA[<strong>LOCATION</strong>]]></fullDescription> 
     <flags> 
     <flag /> 
     </flags> 
     <images> 
     <image modified="2014-05-22 11:10:33">http://link.to/image.jpg</image> 
     </images> 
     <epcFrontPages /> 
     <brochures> 
     <brochure modified="2014-05-22 14:37:38">http://link.to/file.pdf</brochure> 
     </brochures> 
    </property> 
    ....... 
</properties> 

任何幫助非常感謝

+0

能否請您添加XML的例子! –

+0

我很困惑你想要什麼@Ismail,頁面正常工作,因爲它現在只是未排序 - 我可以發佈整個頁面代碼,如果這將有所幫助 –

+0

只是一個XML部分,源XML! –

回答

0

請試試這個:

//Read the xml file 
$xml = simplexml_load_file("http://link.to/file.xml?accesskey"); 
//Get all properties 
$propertylist = $xml->xpath("/properties/property"); 
//Sort them by price (descending) 
usort($propertylist, function($a, $b) { 
    return $b->price - $a->price; 
}); 

//Now you can loop through your ordered `$propertylist`: 
foreach($propertylist as $property) { 
    echo $property->fullDescription . "<br>"; 
} 

演示:https://3v4l.org/Gj120

+0

需要做出幾個變化,只有次要的,雜耍變量名稱,以適應其餘的頁面代碼 謝謝:) :) –

0

控制你的數據到數組

<?php 
$simple = "<para><note>simple note</note></para>"; 
$p = xml_parser_create(); 
xml_parse_into_struct($p, $simple, $vals, $index); 
xml_parser_free($p); 
echo "Index array\n"; 
print_r($index); 
echo "\nVals array\n"; 
print_r($vals); 
?> 

然後排序爲數組 sort array functions