代码:
<?php
$scoreArray = array(
array("id"=>'01',"name"=>'张三丰',"chinese"=>87,"math"=>69,"php"=>98),
array("id"=>'02',"name"=>'李白',"chinese"=>80,"math"=>66,"php"=>68),
array("id"=>'03',"name"=>'陶渊明',"chinese"=>90,"math"=>69,"php"=>78),
);
foreach ( $scoreArray as $key => &$value ){
$value['score'] = $value['chinese'] + $value['math'] + $value['php'];
}
//$arraySort = usort($scoreArray, mySort );
function mySort ($a, $b){
if ($a['score'] == $b['score'])
return 0;
return ($a['score'] > $b['score']) ? -1 : 1;
}
usort($scoreArray, "mySort");
echo "<pre/>";print_r($scoreArray);
结果:
先定义一个空数组a,遍历数组,求出每个人的总成绩记录到数组a里,最后对数组a排序
这些其实用mysql查询的时候做比较合适