我有一个数组,比如xyz,下面是var_dump($ xyz)的输出;
array
7399 =>
array
'count' => int 103
'name' => string '2º SEMESTRE - 2012' (length=23)
7398 =>
array
'count' => int 50
'name' => string '1º SEMESTRE - 2012' (length=23)
7397 =>
array
'count' => int 43
'name' => string '2º SEMESTRE - 2011' (length=23)
7396 =>
array
'count' => int 20
'name' => string '1º SEMESTRE - 2011' (length=23)
7395 =>
array
'count' => int 53
'name' => string '2º SEMESTRE - 2010' (length=23)
'others' =>
array
'name' => string 'Others' (length=6)
'count' => int 65
我想通过count desc对这个数组进行排序,名称asc在底部留下'others'元素。 我使用了下面的array_multisort
// Obtain a list of columns
foreach ($xyz as $key => $row) {
$count[$key] = $row['volume'];
$name[$key] = $row['edition'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($count, SORT_DESC, $name, SORT_ASC, $xyz);
但失败了。它显示以下警告:
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent
想要我想要的是这样的
array
7399 =>
array
'count' => int 103
'name' => string '2º SEMESTRE - 2012' (length=23)
7395 =>
array
'count' => int 53
'name' => string '2º SEMESTRE - 2010' (length=23)
7398 =>
array
'count' => int 50
'name' => string '1º SEMESTRE - 2012' (length=23)
7397 =>
array
'count' => int 43
'name' => string '2º SEMESTRE - 2011' (length=23)
7396 =>
array
'count' => int 20
'name' => string '1º SEMESTRE - 2011' (length=23)
'others' =>
array
'name' => string 'Others' (length=6)
'count' => int 65
请帮忙。感谢所有人的期待!
答案 0 :(得分:1)
此问题已经存在well-written answer:
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
因此,要按计数排序,请执行以下操作:
aasort($your_array,"count");