如何对多维XML文件进行排序? [英] How to sort a multi-dimensional XML file?

查看:83
本文介绍了如何对多维XML文件进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取一个XML文件进行排序,但是没有运气.经过一天半的工作,我需要专家的帮助.谢谢.

I have tried to get an XML file to sort and have had no luck. After a day and a-half, I need some help from an expert. Thanks.

我的XML文件(此示例简称):

My XML File (shortened for the example):

<?xml version="1.0" encoding="iso-8859-1"?>
<deadlines>
    <deadline>
        <date>2010-06-01</date>
        <text>Application for Summer Due</text>
    </deadline>
    <deadline>
        <date>2010-07-01</date>
        <text>Application for Fall Due</text>
    </deadline>
    <deadline>
        <date>2010-07-31</date>
        <text>Summer Bill Due</text>
    </deadline>
</deadlines>

我的PHP:

<?php

$xml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/feeds/deadlines.xml');

// start THIS WORKS
echo'<pre>';
foreach($xml as $deadline) echo <<<EOF
    Date: {$deadline->date}
    Text: {$deadline->text}


EOF;
echo'</pre>';
// end THIS WORKS

?>

有人有一个简单的PHP解决方案在回显到屏幕之前的日期"对XML文件进行排序吗?

Does anyone have a simple PHP solution to sort the XML file on "date" prior to the echo to screen?

谢谢

推荐答案

好的,很抱歉以前在房子里走来走去-为了清楚起见,我添加了一个不同的答案,但使用的是我链接到的排序代理技术.

Okay, sorry for going around the houses before - I've added a different answer for clarity but using the sort proxying technique I linked to.

function xsort(&$nodes, $child_name, $order=SORT_ASC)
{
    $sort_proxy = array();

    foreach ($nodes as $k => $node) {
        $sort_proxy[$k] = (string) $node->$child_name;
    }

    array_multisort($sort_proxy, $order, $nodes);
}

$structure = '<?xml version="1.0" encoding="utf-8" ?>
<deadlines>
    <deadline>
        <date>2010-06-01</date>
        <text>Application for Summer Due</text>
    </deadline>
    <deadline>
        <date>2010-07-01</date>
        <text>Application for Fall Due</text>
    </deadline>
    <deadline>
        <date>2010-07-31</date>
        <text>Summer Bill Due</text>
    </deadline>
</deadlines>';

$xml = simplexml_load_string($structure);
$nodes = $xml->xpath('/deadlines/deadline');

// Sort by date, descending
xsort($nodes, 'date', SORT_DESC);
var_dump($nodes);

这篇关于如何对多维XML文件进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆