使用 Uasort 对多维数组进行排序 [英] Sorting Multidimensional Arrays With Uasort
问题描述
目前我有一些看起来像这样的多维数组
Currently I have some multidimensional arrays that look like this
Array (
[71] => Array ( [author] => 2 [date] => 1392867376 )
[49] => Array ( [author] => 2 [date] => 1392868188 )
[75] => Array ( [author] => 14 [date] => 1392867388)
[67] => Array ( [author] => 2 [date] => 1392870805 )
)
我想按日期"对它们进行排序,但我不知道如何排序.我试过这个:
I would like to sort them by the "date" but I have no idea how. I have tried this:
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
uasort($visited, 'cmp');
但由于我不知道,也找不到关于如何使用比较函数"的参考,我在太空中.我所能找到的只是非常模糊的东西.目前,这是按作者"排序的.
But since I have no idea, and could not find a reference on how to use the "comparison function" I'm up in space. All I've been able to find was very vague stuff. Currently this sorts by "author".
有人可以向我解释这些比较函数是如何工作的(或指向一个在线资源)并告诉我我需要做什么来按日期"对这个数组进行排序 - 同时保持所有键完整(键不得更改或删除)
Can someone kindly explain to me how these comparison functions work (or point me to an online resource) and tell me what I need to do to sort this array by "date" - while keeping all keys intact (keys must not be changed or erased)
非常感谢您提供的任何帮助.
PS:我尝试过 array_multisort - 它删除了我的密钥.
PS: I have tried array_multisort - It erased my keys.
推荐答案
试试这个 cmp 功能:
try this cmp function:
function cmp($a, $b) {
if ($a['date'] == $b['date']) {
return 0;
}
return ($a['date'] < $b['date']) ? -1 : 1;
}
它应该可以工作.
这篇关于使用 Uasort 对多维数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!