我有以下数组:
Array
(
[0] => Array
(
[days] => Wed
[count] => 0
)
[1] => Array
(
[days] => Tue
[count] => 0
)
[2] => Array
(
[days] => Mon
[count] => 1
)
[3] => Array
(
[days] => Sun
[count] => 0
)
我想对它进行排序,所以看起来像这样:
Array
(
[3] => Array
(
[days] => Sun
[count] => 0
)
[2] => Array
(
[days] => Mon
[count] => 1
)
[1] => Array
(
[days] => Tue
[count] => 0
)
[0] => Array
(
[days] => Wed
[count] => 0
)
任何人都可以帮我这么做吗?
答案 0 :(得分:1)
usort($myArray, function($a, $b) {
return $a['days'] - $b['days']; // your operation for checking value
});
答案 1 :(得分:1)
尝试usort
:如果您仍然使用PHP 5.2或更早版本,则必须首先定义排序功能:
function sortByOrder($a, $b) {
return $a['days'] - $b['days'];
}
usort($myArray, 'sortByOrder');
从PHP 5.3开始,您可以使用匿名函数:
usort($myArray, function($a, $b) {
return $a['days'] - $b['days'];
});
最后,使用PHP 7,您可以使用"太空飞船运营商":
usort($myArray, function($a, $b) {
return $a['days'] <=> $b['days'];
});
答案 2 :(得分:1)
function compareOrder($a, $b)
{
return $a['days'] - $b['days'];
}
usort($array, 'compareOrder');
答案 3 :(得分:1)
以下php函数可以提供帮助:
bool array_multisort ( array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $... ]]] )
答案 4 :(得分:0)
可能会帮助你
usort($inputArray, function($first, $second) {
return $first['days'] - $second['days'];
});