Home > php教程 > PHP源码 > php二维数组排序后获取最大值

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

WBOY
Release: 2016-06-08 17:23:37
Original
2623 people have browsed it

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

<script>ec(2);</script>

自定义:

 代码如下 复制代码

function array_sort($arr,$keys,$type='asc'){ //2维数组排序
 $keysvalue = $new_array = array();
 foreach ($arr as $k=>$v){
  $keysvalue[$k] = $v[$keys];
 }
 if($type == 'asc'){
  asort($keysvalue);
 }else{
  arsort($keysvalue);
 }
 reset($keysvalue);
 foreach ($keysvalue as $k=>$v){
  $new_array[$k] = $arr[$k];
 }
 return $new_array;
}

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

 代码如下 复制代码

//$re 为数据来源,二维数组

$re = $this->array_sort($re,'LV','desc');//排序
 foreach($re as $key=>$val){
  foreach ($val as $key2=>$val2){
   if($key2=="uid"){
   $count[] = $val2;//获取创建角色总数
   $lv[$val2][]=$re[$key]['LV'];//获取所有角色等级 
   }
    
  }
 }
 $count = array_count_values($count); 
 foreach($count as $key=>$val){
  M()->query("update mj_ad_count set create_juese = create_juese+".$val." where suid =".$key);
  echo M()->getLastSql();  
 }
 foreach($lv as $key =>$val){//等级排查找出最大值后写入统计
  foreach ($val as $key2=> $val2){
   if($key2==0){
    //$ls[$key][] = $val2;
    M()->query("update mj_ad_count set juese_leave = ".$val2." where suid =".$key);
    echo M()->getLastSql(); 
   }   
  }
 }

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template