如何按嵌套 php 数组排序
这是我正在访问的 xml feed 的摘录:
这是我当前的代码:
$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()
函数(例如 这个答案),但这仍然有点超出我的范围,我不知道如何让它在这里工作。
更新
我正在玩@克里斯·戈达德的回答如下..
这是我得到的代码,但它似乎没有达到目的:
<?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>";
?>
我做错了什么?
This is an excerpt from the xml feed I'm accessing:
And here's my current code:
$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>
This is the output:
My question is, how do I get the table to sort in descending numerical order (i.e. most recent year first)? I've searched here and tried to understand the sort()
function (e.g. this answer), but it's a bit beyond me still and I can't figure out how to get that to work here.
UPDATE
I'm playing around with @Chris Goddard's answer below..
This is the code I've got, but it doesn't seem to have done the trick:
<?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>";
?>
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不明白该代码的一半(所有正则表达式是做什么用的?),但要实现所需的排序表,您可以简单地执行以下操作:
加载 XML 并获取 XML 中出现的所有产品的列表。要对它们进行排序,您可以使用自定义排序:
它将比较 simplexml 元素的开始日期并按降序对它们进行排序。然后只需迭代产生式即可:
查看演示
另请参阅此 针对您的其他请求的演示,位于 聊天,例如将作品限制为那些有评论并按作品开始日期排序的作品。
I dont understand half of that code (what's all the regex for?), but to achieve desired sorted table you can simply do:
to load the XML and get the list of all productions as they appear in the XML. To sort them you can use a custom sort:
which will compare the startdates of the simplexml elements and sort them in descending order. And then it's only a matter of just iterating the productions:
See demo
Also see this demo for your additional requests in chat, e.g. limit productions to those with a review and sorting works by production startdate.
技巧
array_multisort 将完成将数组放入第一个数组的
(其中包含每个元素的键、排序值、方向,然后是大数组) 编辑
array_multisort will do the trick
you put an array in the first one (which has the keys of each element, and the sorting value, the direction then the big array)
Edit
您可以将值放入数组中,然后使用 usort 按用户定义的函数进行排序。
请注意,当元素被设置到数组中时,它们会被转换为字符串。将它们与 XML 对象进行比较并不是您想要的。
一点解释:
首先,我将 XML 数据放入一个数组中,只是因为这样数据更容易使用。
然后,我根据自定义函数
date_sort
对数组进行排序。查看 usort 的文档,您会看到:所以,因为你想按日期降序排序,如果日期相等,则排序顺序也相等(返回 0),如果日期较晚,则它应该在列表中较早(返回 -1),如果日期越大,它应该在列表中的后面(返回 1),
然后您只需遍历数组打印即可。
如果您想使用其他数据,只需将其放入第一个循环的数组中即可。
you can place the values in an array, then use usort to sort by a user defined function.
And notice that the elements are cast to string when they are set in the array. Comparing them as the XML objects they are isn't what you want.
A little explanation:
First I'm placing the XML data into an array, just because the data is easier to work with that way.
Then, I sort the array based on my custom function
date_sort
. Looking at the documentation for usort you see:So, because you want to sort by date desc, if the dates are equal then the sort order is also equal(return 0), if the date is later it should be earlier in the list (return -1), and if the date is greater it should be later in the list (return 1)
Then you just traverse through the array printing as you go.
If you want to use additional data, just place it in the array in the first loop.