【发布时间】:2013-01-03 03:08:04
【问题描述】:
我有一个数组,比如说 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
我想按计数 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
请帮忙。谢谢大家的期待!!!
【问题讨论】:
-
你试过
usort()吗? -
@romainberger:usort 使用用户定义的函数对数组进行排序。如果我能够定义一个可以对数组进行排序/解决我的问题的函数,我为什么要向她发布问题。无论如何,感谢您的回复
标签: php multidimensional-array sorting