PHP数组使用子节点排序?
好的,我有以下数组:)
Array
(
[0] => Array
(
[id] => 6
[name] => This Course
[time] => 1288082700
[description] => blah blah .
[link] => http://this.com/?g=5
[course] => 22
)
[1] => Array
(
[id] => 2
[name] => Workshop
[time] => 1287561600
[description] => This description
[link] => http://this.com/?g=5
[session] => 8
[course] => 23
[type] => standard
[adobelink] =>
)
如何
使用内部“时间”键对整个数组进行排序?
谢谢!
Ok I have the following array:
Array
(
[0] => Array
(
[id] => 6
[name] => This Course
[time] => 1288082700
[description] => blah blah .
[link] => http://this.com/?g=5
[course] => 22
)
[1] => Array
(
[id] => 2
[name] => Workshop
[time] => 1287561600
[description] => This description
[link] => http://this.com/?g=5
[session] => 8
[course] => 23
[type] => standard
[adobelink] =>
)
)
How can I sort this entire array by using the inner 'time' key ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将
usort
函数用作:工作链接
You can use the
usort
function as:Working link
使用
usort()
:示例:
当然,如果数组没有
time
元素,这将会失败。接下来会发生什么取决于您的要求,所以我将把错误处理留给您;)Use
usort()
:Example:
Of course this will fail if an array has no
time
element. What should happen then depends on your requirements so I will leave the error handling to you ;)使用 PHP usort() 函数: http://php.net/manual/en/function。 usort.php
首先,定义一个函数来决定数据结构的比较结果:
然后调用 usort() 并将函数的 nmae 给它:
你就完成了!
Use PHP usort() function: http://php.net/manual/en/function.usort.php
First, define a function that will decide on compare result for your data structure:
Then call usort() and give the nmae of your function to it:
You're done!
uasort()
将 维护您的密钥1。匿名函数语法需要 PHP 5.3+!如果 <5.3,则传递比较函数的名称(请参阅其他答案)。
1) 如果您也关心按键。如果没有,只需使用上面大量使用的
usort()
方法:) 比较函数基本相同(@codaddict 的 优雅的方法)。uasort()
will maintain your keys1.Anonymous function syntax requires PHP 5.3+! Pass the name of the comparison function if <5.3 (see other answers).
1) In case you care about the keys, too. If not, just use the
usort()
approach found in abundance above :) The comparison function are basically identical (except for @codaddict's elegant approach).