我是PHP的新手。我有一个二维PHP数组。"内部"数组有一个值,我想要根据它进行排序。
例如:
$myarray[1]['mycount']=12
$myarray[2]['mycount']=13
$myarray[3]['mycount']=9
我希望您能帮我将“内部”数组按降序排序。
因此,以下结果将为13、12、9。
foreach ($myarray as $myarr){
print $myarr['mycount']
}
感谢您的提前帮助。
usort();
函数按照用户定义的比较方式进行排序。// 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>";