duanemei2194 2011-11-25 21:03
浏览 28
已采纳

按子节点PHP SimpleXML排序xml div

so I have a list of <div>'s in an xml file. I'm parsing the file using php's simpleXML

I can generate an array of all the divs with the following:

$divArray = $xmldoc->text->body->children();

But now I would like to order the $divArray by different childNodes (author, title, date) within the div.

The div looks like this.

<div>
    <bibl>
       <author>
       <title>
       <date>
   </bibl>
</div>

So how can I take $divArray and sort it by <author> or <title> or <date>?

Thanks for your help. jw

  • 写回答

1条回答 默认 最新

  • dongshuogai2343 2011-11-25 21:52
    关注

    The basic procedure is

    1. cast a SimpleXMLElement into an array
    2. write a comparison function that accepts two SimpleXMLElement arguments
    3. sort the array with the comparison function using usort()

    I can only guess at your original XML structure, but I think it looks something like this:

    $xml = <<<EOT
    <root>
    <text>
        <body>
            <div>
                <bibl>
                    <author>A</author>
                    <title>A</title>
                    <date>1</date>
                </bibl>
            </div>
            <div>
                <bibl>
                    <author>B</author>
                    <title>B</title>
                    <date>2</date>
                </bibl>
            </div>
            <div>
                <bibl>
                    <author>D</author>
                    <title>D</title>
                    <date>4</date>
                </bibl>
            </div>
            <div>
                <bibl>
                    <author>C</author>
                    <title>C</title>
                    <date>3</date>
                </bibl>
            </div>
        </body>
    </text>
    </root>
    EOT;
    
    $xmldoc = new SimpleXMLElement($xml);
    

    Step 1: Cast to array. Note that your $divArray is not actually an array!

    $divSXE = $xmldoc->text->body->children(); // is a SimpleXMLElement, not an array!
    // print_r($divSXE);
    $divArray = array();
    foreach($divSXE->div as $d) {
        $divArray[] = $d;
    }
    // print_r($divArray);
    

    Step 2: write a comparison function. Since the array is a list of SimpleXMLElements, the comparison function must accept SimpleXMLElement arguments. SimpleXMLElements need explicit casting to get string or integer values.

    function author_cmp($a, $b) {
        $va = (string) $a->bibl->author;
        $vb = (string) $b->bibl->author;
        if ($va===$vb) {
            return 0;
        }
        return ($va<$vb) ? -1 : 1;
    }
    

    Step 3: Sort the array with usort()

    usort($divArray, 'author_cmp');
    print_r($divArray);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)