我正在像这样浏览XML树:
$notesXML = simplexml_load_string(XMLSTRING);
foreach($notesXML as $thenote){
$noteAttr = $thenote->attributes();
echo $noteAttr['modified'];
}
可以看到,XML树中有一个名为"modified"的属性,我现在要做的是根据修改的日期按升序或降序打印出XML树。顺便说一句,日期字符串的格式如下:"Tuesday 6th of September 2011 03:49:14 PM"
感谢您的帮助
您可以为要排序的元素构建一个数组,然后使用其中一个数组排序函数对它们重新排序。
下面的代码片段使用array_multisort()
按日期降序对它们进行排序。DateTime::createFromFormat()
用于从日期字符串中获取Unix时间戳。
$notes = array();
$dates = array();
foreach ($notesXML as $note) {
$notes[] = $note;
$dates[] = DateTime::createFromFormat('l jS 'of F Y H:i:s A', $note['modified'])->getTimestamp();
}
array_multisort($dates, SORT_DESC, $notes);
// Loop over $notes however you like