使用 php 在多维数组中按键排序
可能的重复:
在 PHP 中对多维数组进行排序
如何在多维数组中按键排序?
例如,下面是我从数据库打印的数组,其中最新排在前面 - 12月、11月、10月等和2011年、2010年、2009年,等等
Array
(
[0] => Array
(
[URL] => september 2011
[Title] => September 2011
[Date] => 8
[Month] => 9
[Year] => 2011
)
[1] => Array
(
[URL] => january 2011
[Title] => January 2011
[Date] => 1
[Month] => 2
[Year] => 2011
)
[2] => Array
(
[URL] => february 2011
[Title] => February 2011
[Date] => 4
[Month] => 1
[Year] => 2011
)
[3] => Array
(
[URL] => november 2011
[Title] => November 2011
[Date] => 23
[Month] => 11
[Year] => 2010
)
[4] => Array
(
[URL] => april 2011
[Title] => April 2011
[Date] => 23
[Month] => 4
[Year] => 2010
)
)
但我需要它像这样,十月、十一月、十二月等和2011年、2010年、2009年等 - 请注意,月份按最旧在前排序,但年份仍按最新在前排序。
所以数组应该这样排序,
Array
(
[2] => Array
(
[URL] => february 2011
[Title] => February 2011
[Date] => 4
[Month] => 1
[Year] => 2011
)
[1] => Array
(
[URL] => january 2011
[Title] => January 2011
[Date] => 1
[Month] => 2
[Year] => 2011
)
[0] => Array
(
[URL] => september 2011
[Title] => September 2011
[Date] => 8
[Month] => 9
[Year] => 2011
)
[4] => Array
(
[URL] => april 2010
[Title] => April 2010
[Date] => 23
[Month] => 4
[Year] => 2010
)
[3] => Array
(
[URL] => november 2010
[Title] => November 2010
[Date] => 23
[Month] => 11
[Year] => 2010
)
)
这可能吗?
Possible Duplicate:
Sorting multidimensional array in PHP
How can I sort by key in a multidimensional array?
For instance, below is the array I print from my db, where the latest comes first - December, November, October, etc and 2011, 2010, 2009, etc
Array
(
[0] => Array
(
[URL] => september 2011
[Title] => September 2011
[Date] => 8
[Month] => 9
[Year] => 2011
)
[1] => Array
(
[URL] => january 2011
[Title] => January 2011
[Date] => 1
[Month] => 2
[Year] => 2011
)
[2] => Array
(
[URL] => february 2011
[Title] => February 2011
[Date] => 4
[Month] => 1
[Year] => 2011
)
[3] => Array
(
[URL] => november 2011
[Title] => November 2011
[Date] => 23
[Month] => 11
[Year] => 2010
)
[4] => Array
(
[URL] => april 2011
[Title] => April 2011
[Date] => 23
[Month] => 4
[Year] => 2010
)
)
But I need it to be like this, October, November, December, etc and 2011, 2010, 2009, etc - note the months are sorted by the oldest comes first but the years are still sorted by the latest comes first.
So the array should be sorted like this,
Array
(
[2] => Array
(
[URL] => february 2011
[Title] => February 2011
[Date] => 4
[Month] => 1
[Year] => 2011
)
[1] => Array
(
[URL] => january 2011
[Title] => January 2011
[Date] => 1
[Month] => 2
[Year] => 2011
)
[0] => Array
(
[URL] => september 2011
[Title] => September 2011
[Date] => 8
[Month] => 9
[Year] => 2011
)
[4] => Array
(
[URL] => april 2010
[Title] => April 2010
[Date] => 23
[Month] => 4
[Year] => 2010
)
[3] => Array
(
[URL] => november 2010
[Title] => November 2010
[Date] => 23
[Month] => 11
[Year] => 2010
)
)
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用多个键对数组的数组进行排序的通用解决方案
根据我对这个问题的回答,这是一个非常通用的解决方案,您可以使用它可以在很多情况下使用。
限制:由于存在匿名函数,需要 PHP >= 5.3 才能工作。
新增和改进,现在支持降序排序
如何使用它
按年份升序排序:
按年份升序排序,然后按月份升序排序:
按年份降序排序,然后按月份升序排序:
最后一个就是您想要的。
Generic solution to sort arrays of arrays with multiple keys
Based on my answer to this question, here is a very generic solution that you can use in lots of situations.
Limitation: Requires PHP >= 5.3 to work, due to the presence of anonymous functions.
New and improved, now with descending sort support
How to use it
To sort by year ascending:
To sort by year ascending, then by month ascending:
To sort by year descending, then by month ascending:
This last one is what you 're after.
假设您在问题中提供的数据实际上是正确的(以年、月、日为基础来创建排序),您可以首先按这些值索引数组并对主数组进行排序。我只是写它,否则你可能会误读 Demo 的输出,URL/标题不对应给定的数值,但它只是有效:
Assuming that the data you would have provided in your question would have been actually correct (taking Year, Month, Date as base to create a sort on), you can first index the array by those values and the sort the main array. I'm just writing it, otherwise you might misread the output of the Demo, the URL/Titles do not correspond to the numerical values given, but it just works:
怎么样:
数据:
代码:
输出:
How about:
Data:
code:
output: