php 按子值对php多维数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4508145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort php multidimensional array by sub-value
提问by Mr Hyde
I have this array
我有这个数组
Array
(
[data] => Array
(
[0] => Array
(
[id] => 1293005125
[viewed] => TRUE
[active] => TRUE
[time] => December 22, 2010 13:00 hours
[timestamp] => 1293006034
[initial_timestamp] => 1293005125
[user] => administrator
)
[1] => Array
(
[mid] => 1293001908
[viewed] => TRUE
[active] => TRUE
[time] => December 22, 2010 13:00 hours
[timestamp] => 1293001908
[initial_timestamp] => 1293001908
[user] => administrator
)
[2] => Array
(
[mid] => 1293009999
[viewed] => TRUE
[active] => TRUE
[time] => December 22, 2010 13:00 hours
[timestamp] => 1293009999
[initial_timestamp] => 1293009999
[user] => administrator
)
[3] => Array
(
[mid] => 1293006666
[viewed] => TRUE
[active] => TRUE
[time] => December 22, 2010 13:00 hours
[timestamp] => 1293006666
[initial_timestamp] => 1293006666
[user] => administrator
)
[4] => Array
(
[mid] => 1293005125
[viewed] => TRUE
[active] => TRUE
[time] => December 22, 2010 13:00 hours
[timestamp] => 1293006125
[initial_timestamp] => 1293005125
[user] => administrator2
)
)
Now I would like to sort this array by [mid]
How do I do this?
现在我想通过[mid]
如何执行此操作对这个数组进行排序?
Currently I sort this in a foreach loop
There has to be a better way
目前我在 foreach 循环中对此进行排序
必须有更好的方法
EDITI hoped to output something like
编辑我希望输出类似
[mid] key => array value
[mid] key => array value
Thanks
谢谢
回答by codaddict
回答by Mukesh Chapagain
The other solution is using array_multisort
另一个解决方案是使用array_multisort
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
$mid[$key] = $row['mid'];
}
// Sort the data with mid descending
// Add $data as the last parameter, to sort by the common key
array_multisort($mid, SORT_DESC, $data);
?>
回答by Jon
Update
更新
I recently answeredthis question in a much more capable manner in the "definitive" topic on sorting multidimensional arrays. The answer below targets old versions of PHP (5.2 and earlier); while the concept is sound, nowadays there are much better ways of doing things. Read the answers on other question instead.
我最近在关于对多维数组进行排序的“确定性”主题中以更有能力的方式回答了这个问题。下面的答案针对旧版本的 PHP(5.2 及更早版本);虽然这个概念是合理的,但现在有更好的做事方式。请阅读其他问题的答案。
Original (very outdated) answer
原始(非常过时)答案
usort
is there for exactly this situation. If you also need keys to be preserved, the appropriate function would be uasort
.
usort
是不是针对这种情况。如果您还需要保留密钥,则适当的功能是uasort
.
For example:
例如:
usort($array, create_function('$a, $b',
'if ($a["mid"] == $b["mid"]) return 0; return ($a["mid"] < $b["mid"]) ? -1 : 1;'));
Of course if you don't mind, you can declare the comparison function properly:
当然,如果你不介意,你可以正确声明比较函数:
function compareMid($a, $b)
{
if ($a['mid'] == $b['mid']) {
return 0;
}
return ($a['mid'] < $b['mid']) ? -1 : 1;
}
And use it like this:
并像这样使用它:
usort($array, 'compareMid');
All of this is in the documentation.
所有这些都在文档中。