doushi2845 2015-05-18 14:40
浏览 99
已采纳

PHP:按多个元素对XML节点列表进行排序

Beginner programmer here.

I am in php, working with a dom document nodelist comprising of selected nodes retrieved from an XPath query on an xml file. Excerpt of contents of nodelist:

<properties>
    <property>
        <price>270</price>
        <address>3/2 Farran Street, Castlemaine, VIC, Australia</address>
        <infants>3</infants>
    </property>
    <property>
        <price>250</price>
        <address>2/37 William Street, Castlemaine, VIC, Australia</address>
        <infants>2</infants>
    </property>
    <property>
        <price>250</price>
        <address>2/37 William Street, Castlemaine, VIC, Australia</address>
        <infants>3</infants>
    </property>
    ...

I wish to sort the nodelist by price ascending, and in the case of matching prices, by no of infants descending. In the above snippet, for example, the result would be:

<property>
    <price>250</price>
    <address>2/37 William Street, Castlemaine, VIC, Australia</address>
    <infants>3</infants>
</property>
<property>
    <price>250</price>
    <address>2/37 William Street, Castlemaine, VIC, Australia</address>
    <infants>2</infants>
</property>
<property>
    <price>270</price>
    <address>3/2 Farran Street, Castlemaine, VIC, Australia</address>
    <infants>3</infants>
</property>

I have seen some sorting questions answered relating to multi-dimensional arrays but I can't seem to translate these to nodelists. I can sort by price ok (see below), but I can't determine how to then sort by infants.

$properties = iterator_to_array($matches); // where matches = a return from an XPath query
usort($properties, 'sortByPrice');

function sortByPrice ($a, $b) {
    $aPrice = $a->getElementsByTagName("price");
    $aPrice = $aPrice->item(0)->nodeValue;
    $bPrice = $b->getElementsByTagName("price");
    $bPrice = $bPrice->item(0)->nodeValue;
    return (int) $aPrice - (int) $bPrice;
}
  • 写回答

3条回答

  • duanmao1975 2015-05-18 15:17
    关注

    if your code works ok for price, why not just extend it:

    $properties = iterator_to_array($matches); // where matches = a return from an XPath query
    usort($properties, 'sortByPrice');
    
    function sortByPrice ($a, $b) {
        $aPrice = $a->getElementsByTagName("price");
        $aPrice = $aPrice->item(0)->nodeValue;
        $bPrice = $b->getElementsByTagName("price");
        $bPrice = $bPrice->item(0)->nodeValue;
        $r = (int) $aPrice - (int) $bPrice;
        if (!$r) {
           $ainfants = $a->getElementsByTagName("infants");
           $ainfants = $ainfants->item(0)->nodeValue;
           $binfants = $b->getElementsByTagName("infants");
           $binfants = $bPrice->item(0)->nodeValue;
           $r = (int) $ainfants - (int) $binfants;
        }
        return $r;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料