我试图对一个 XML 文件进行排序,但没有成功.一天半后,我需要专家的帮助.谢谢.
我的 XML 文件(示例中的缩写):
<截止日期><截止日期><日期>2010-06-01</日期><text>夏季到期申请</text></截止日期><截止日期><日期>2010-07-01</日期><text>秋季到期申请</text></截止日期><截止日期><日期>2010-07-31</日期><text>夏季账单到期</text></截止日期></截止日期>我的 PHP:
';foreach($xml as $deadline) echo <<<EOF日期:{$deadline->date}文本:{$deadline->text}EOF;echo'</pre>';//结束这项工作?>有没有人有一个简单的 PHP 解决方案来在回显到屏幕之前按日期"对 XML 文件进行排序?
谢谢
好的,抱歉之前在房子里走来走去 - 为了清楚起见,我添加了不同的答案,但使用了我链接到的排序代理技术.
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" ?><截止日期><截止日期><日期>2010-06-01</日期><text>夏季到期申请</text></截止日期><截止日期><日期>2010-07-01</日期><text>秋季到期申请</text></截止日期><截止日期><日期>2010-07-31</日期><text>夏季账单到期</text></截止日期></deadlines>';$xml = simplexml_load_string($structure);$nodes = $xml->xpath('/deadlines/deadline');//按日期排序,降序xsort($nodes, 'date', SORT_DESC);var_dump($nodes);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.
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>
My 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
?>
Does anyone have a simple PHP solution to sort the XML file on "date" prior to the echo to screen?
Thanks
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 文件进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 SELECT(MYSQL/PHP) 中加入 2 个表Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 个表)
如何使<option selected=“selected">由How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 设置?)
使用 PHP 中的数组自动填充选择框Auto populate a select box using an array in PHP(使用 PHP 中的数组自动填充选择框)
PHP SQL SELECT where like search item with multiple wordsPHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode 从 MSSQL-SELECT 产生 JSON_ERROR_UTF8json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 从 MSSQL-SELECT 产生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(),名称 ASCMySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名称 ASC)