作者:火云邪神 | 来源:互联网 | 2023-12-09 10:14
php二维数组排序后获取最大值
PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义.
PHP自定义代码如下:
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();
}
}
}