我有一个数组,如下:
Array
(
[0] => Array
(
[record_id] => 21
[quality] => 3
)
[1] => Array
(
[record_id] => 20
[quality] => 3
)
[2] => Array
(
[record_id] => 19
[quality] => 3
)
[3] => Array
(
[record_id] => 18
[quality] => 2
)
[4] => Array
(
[record_id] => 17
[quality] => 3
)
)
我需要能够以升序或降序(当选择模式时)从最高到最低(关于quality
键)排序数组,例如我需要它来订购一个数组并返回它有序,例如
function order_array($array, $order = 'asc')
{
if($order == 'asc')
// order the array from lowest to highest
if($order == 'desc')
// order the array from highest to lowest
}
干杯。
答案 0 :(得分:4)
检查usort功能
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
答案 1 :(得分:1)
使用函数usort
:
usort($array, "my_custom_sort_function");
function my_custom_sort_function($a, $b) {
return $a['quality'] - $b['quality'];
}