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

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(); 
   }   
  }
 }

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version