This question already has an answer here:
这个问题已经有了答案:
- Sort a set of multidimensional arrays by array elements 2 answers
- 根据数组元素2对一组多维数组进行排序
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循环中对它进行排序,必须有更好的方法
EDIT I hoped to output something like
编辑我希望输出一些类似的东西。
[mid] key => array value
[mid] key =>数组值
Thanks
谢谢
3 个解决方案
#1
#2
32
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);
?>
#3
5
Update
I recently answered this 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
.
尤索特就是为了这种情况。如果还需要保留键,则适当的函数是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.
所有这些都在文档中。
#1
53
You can use the usort function.
你可以使用usort函数。
function cmp($a, $b) {
return $a["mid"] - $b["mid"];
}
usort($arr, "cmp");
看到它
#2
32
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);
?>
#3
5
Update
I recently answered this 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
.
尤索特就是为了这种情况。如果还需要保留键,则适当的函数是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.
所有这些都在文档中。