我想根据现场年份对以下2D数组进行排序?
Array
(
[0] => Array
(
[from] => 44.91
[to] => 53.56
[next] => 108.88
[year] => 2011
),
[1] => Array
(
[from] => 44.44
[next] => 53.16
[year] => 2010
),
[2] => Array
(
[from] => 42.83
[next] => 51.36
[year] => 2012
)
);
所以期望的结果将是:
Array
(
[1] => Array
(
[from] => 44.44
[next] => 53.16
[year] => 2010
),
[0] => Array
(
[from] => 44.91
[to] => 53.56
[next] => 108.88
[year] => 2011
),
[2] => Array
(
[from] => 42.83
[next] => 51.36
[year] => 2012
)
);
答案 0 :(得分:1)
您是否尝试过使用uasort()
?
function cmp_year($a, $b) {
if($a['year'] == $b['year']) return 0;
return ($a['year'] < $b['year']) ? -1 : 1;
}
uasort($your_array, 'cmp_year');
答案 1 :(得分:1)
您可以使用array_multisort()
。
foreach ($arr as $k => $v) {
$years[$k] = $v['year'];
}
array_multisort($years, SORT_DESC, $arr);