2016-07-30 54 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