二维数组排序 [英] Sorting a Two-Dimensional Array
本文介绍了二维数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是PHP新手.我有一个二维的PHP数组. 内部"数组具有我要排序的值.
I am new to PHP. I have a PHP array that is two dimensional. The "inner" array has a value that I want to sort on.
例如:
$myarray[1]['mycount']=12
$myarray[2]['mycount']=13
$myarray[3]['mycount']=9
我想按降序对内部"数组进行排序.
I want to sort the "inner" array in descending order.
所以以下结果将是13、12、9
So the results for the following will be 13, 12, 9
foreach ($myarray as $myarr){
print $myarr['mycount']
}
提前谢谢.
推荐答案
您可以使用 usort();
按用户定义的比较进行排序.
You can use usort();
to sort by a user-defined comparison.
// Our own custom comparison function
function fixem($a, $b){
if ($a["mycount"] == $b["mycount"]) { return 0; }
return ($a["mycount"] < $b["mycount"]) ? -1 : 1;
}
// Our Data
$myarray[0]['mycount']=12
$myarray[1]['mycount']=13
$myarray[2]['mycount']=9
// Our Call to Sort the Data
usort($myArray, "fixem");
// Show new order
print "<pre>";
print_r($myArray);
print "</pre>";
这篇关于二维数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文