php二维数组排序后获取最大值

PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义.

PHP自定义代码如下:

  1. function array_sort($arr,$keys,$type='asc'){ //2维数组排序
  2. $keysvalue = $new_array = array();
  3. foreach ($arr as $k=>$v){
  4. $keysvalue[$k] = $v[$keys];
  5. }
  6. if($type == 'asc'){
  7. asort($keysvalue);
  8. }else{
  9. arsort($keysvalue);
  10. }
  11. reset($keysvalue);
  12. foreach ($keysvalue as $k=>$v){
  13. $new_array[$k] = $arr[$k];
  14. }
  15. return $new_array;
  16. }

最近在做一个数据分析系统,需要用到各种排序,下面是一段排序,代码如下:

  1. //$re 为数据来源,二维数组
  2. $re = $this->array_sort($re,'LV','desc');//排序
  3. foreach($re as $key=>$val){
  4. foreach ($val as $key2=>$val2){
  5. if($key2=="uid"){
  6. $count[] = $val2;//获取创建角色总数
  7. $lv[$val2][]=$re[$key]['LV'];//获取所有角色等级
  8. }
  9. }
  10. }
  11. $count = array_count_values($count);
  12. foreach($count as $key=>$val){
  13. M()->query("update mj_ad_count set create_juese = create_juese+".$val." where suid =".$key);
  14. echo M()->getLastSql();
  15. }
  16. foreach($lv as $key =>$val){//等级排查找出最大值后写入统计
  17. foreach ($val as $key2=> $val2){
  18. if($key2==0){
  19. //$ls[$key][] = $val2;
  20. M()->query("update mj_ad_count set juese_leave = ".$val2." where suid =".$key);
  21. echo M()->getLastSql();
  22. }
  23. }
  24. }