php对数组重新排序
我有一系列按年份和月份分隔的条目,如下所示。我需要对这个数组进行排序,以便条目按年份“分组”,然后按类别“分组”。
Array
(
[Oct 2011] => Array
(
[0] => Array
(
[title] => CAD Drawing Updates
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
)
[Sep 2011] => Array
(
[0] => Array
(
[title] => title
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
[1] => Array
(
[title] => edges
[file] => /gm-June2010-driver.pdf
[category] =>Walling
)
[2] => Array
(
[title] => Specification update
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
)
)
这就是我所追求的东西。
Array
(
[Oct 2011] => Array
(
[Windows] => Array
(
[0] => Array
(
[title] => CAD Drawing Updates
[file] => /gm-June2010-driver.pdf
)
)
)
[Sep 2011] => Array
(
[Windows] => Array
(
[0] => Array
(
[title] => title
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
[1] => Array
(
[title] => Specification update
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
)
[Walling] => Array
(
[0] => Array
(
[title] => edges
[file] => /gm-June2010-driver.pdf
[category] => Curtain Walling
)
)
)
)
我不确定这是否是排序功能的工作,任何帮助将不胜感激,谢谢。
I have an array of entries that are separated by year and month like below. I need to sort this array so that entries are "grouped" by year and then by category.
Array
(
[Oct 2011] => Array
(
[0] => Array
(
[title] => CAD Drawing Updates
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
)
[Sep 2011] => Array
(
[0] => Array
(
[title] => title
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
[1] => Array
(
[title] => edges
[file] => /gm-June2010-driver.pdf
[category] =>Walling
)
[2] => Array
(
[title] => Specification update
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
)
)
So this is the sort of thing I'm after.
Array
(
[Oct 2011] => Array
(
[Windows] => Array
(
[0] => Array
(
[title] => CAD Drawing Updates
[file] => /gm-June2010-driver.pdf
)
)
)
[Sep 2011] => Array
(
[Windows] => Array
(
[0] => Array
(
[title] => title
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
[1] => Array
(
[title] => Specification update
[file] => /gm-June2010-driver.pdf
[category] => Windows
)
)
[Walling] => Array
(
[0] => Array
(
[title] => edges
[file] => /gm-June2010-driver.pdf
[category] => Curtain Walling
)
)
)
)
I'm not sure if this is a job for the sort functions, any help would be appreciated, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,这不能用排序函数来完成,您需要通过使用嵌套的 foreach 循环迭代原始数组来创建一个新数组。
No, that can't be done with the sort function, you need to create a new array by iterating through your original array with nested foreach loops.
您不想排序,而只是重命名键。 PHP 数组不支持直接重命名键,因此您需要先创建一个使用新键构建的新数组,同时维护现有值。
最后,您可以用新数组替换原始数组:
输出:
Demo
You don't want to sort but just to rename keys. PHP Arrays do not support to rename keys directly, so you need to create a new array first that is build with the new keys while maintaining the existing values.
At the end you can replace the original array with the new one:
Output:
Demo
我认为您应该关注的功能是 uasort。
http://www.php.net/manual/en/function.uasort.php
首先对主数组进行排序,然后对数组中的每个元素对子数组进行 usort。
I think the function you should be looking at is uasort.
http://www.php.net/manual/en/function.uasort.php
First do a sort on the main array, the for each element in the array, do a usort on the child array.