【发布时间】:2011-12-07 07:10:00
【问题描述】:
这是the xml feed I'm accessing的摘录:
这是我当前的代码:
$file = file_get_contents('feed.xml');
$file = preg_replace('/(<role[^>]+>)([^<]+)/si', '$1', $file);
$xml = new SimpleXMLElement($file);
$search_term = preg_replace('/[,.\/\\\(\)\[\]\{\}`~!@#\$%\^&*;:\'"\-_<>]*/is', '', $_GET['work']);
$productions = $xml->xpath('//production');
?>
<table width="300px" cellspacing="5px" cellpadding="5px" border="1px" >
<tr>
<th>year</th>
<th>company</th>
</tr>
<?php
foreach($productions as $prod) {
$prod_attrs = $prod->attributes();
$prod_date = $prod_attrs->startdate;
echo "<tr><td>", $prod_date, "</td><td>", html_encode($prod_attrs->company), "</td></tr>";
}
?>
</table>
这是输出:
我的问题是,如何让表格按数字降序排序(即最近一年优先)?我在这里搜索并试图理解sort() 函数(例如this answer),但它仍然有点超出我的能力,我无法弄清楚如何让它在这里工作。
更新
我在玩@Chris Goddard's answer below..
这是我得到的代码,但它似乎没有成功:
<?php
function html_encode($var){
$var = html_entity_decode($var, ENT_QUOTES, 'UTF-8');
$var = htmlentities($var, ENT_QUOTES, 'UTF-8');
return $var;
}
$file = file_get_contents('feed.xml');
$file = preg_replace('/(<role[^>]+>)([^<]+)/si', '$1', $file);
$xml = simplexml_load_string($file);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
$search_term = preg_replace('/[,.\/\\\(\)\[\]\{\}`~!@#\$%\^&*;:\'"\-_<>]*/is', '', $_GET['work']);
$works = $xml->xpath('//work');
foreach($works as $work) {
$Productions = $work->xpath('./production');
$Sorter = array();
foreach ($Productions as $prod) {
$prod_attrs = $prod->attributes();
$Sorter[$key] = $prod_attrs->startdate;
array_multisort($Sorter, SORT_DESC, $Productions);
}
}
echo "<pre>".print_r($works, true)."</pre>";
?>
我做错了什么?
【问题讨论】:
标签: php sorting html-table