如何在php中排序xml文件

时间:2013-10-23 04:06:10

标签: jquery xml

我试图在php中对我的xml进行排序。我在这里找到了一个解决方案:Sorting XML File into ascending order using php

但是,如果有项目51390等属性,其中有2个课程,而其他属性有1个,则解决方案会显示所有属性。因为在数组中,他只插入

'course'         => (string)$item->courses->course[0]

我的xml也是,它也有其他没有的属性。如何排序我的xml并显示所有内容?欢呼声。

1 个答案:

答案 0 :(得分:0)

你需要使用usort。这应该有效:

<?php
$url = '001.XML';
$xml = simplexml_load_file($url);

$items = array();
foreach($xml->Images->ImageGalleryEntry as $item) {
    $items[] = $item;
};

// Custom sort on the names of the items:
usort ($items, function($a, $b) {
    return strcmp($a['Name'], $b['Name']);
});

foreach ($items as $item) {
    echo $item['FileName'] . "<br/>" . $item['Name'] . "<br/>" . $item['Description'] . "<br/><br/>";
}