使用两个PHP提供的函数:array_column 、 array_multisort
1、array_column() 返回输入数组中某个单一列的值。(引用手册)
语法:array_column(array,column_key,index_key);
array:必需。规定要使用的多维数组(记录集)。
column_key:必需。需要返回值的列。可以是索引数组的列的整数索引,或者是关联数组的列的字符串键值。该参数也可以是 NULL,此时将返回整个数组(配合 index_key 参数来重置数组键的时候,非常有用)。
index_key:可选。用作返回数组的索引/键的列。
例:从记录集中取出 last_name 列,用相应的 "id" 列作为键值:
<?php // 表示由数据库返回的可能记录集的数组 $a = array( array( 'id' => 5698, 'first_name' => 'Bill', 'last_name' => 'Gates', ), array( 'id' => 4767, 'first_name' => 'Steve', 'last_name' => 'Jobs', ) array( 'id' => 3809, 'first_name' => 'Mark', 'last_name' => 'Zuckerberg', ) ); $last_names = array_column($a, 'last_name', 'id'); print_r($last_names); ?>
输出:
Array ( [5698] => Gates [4767] => Jobs [3809] => Zuckerberg )
2、下面开始使用这两个函数实现排序
$department_level1 = Department::getList(array("level"=>1));//得到一个二维数组
$count=0;
$has_done=0;
$satisfaction = 0;
$processing = 0;
foreach ($department_level1 as $key => $value){ $count += handing::getCountById( $value['id'], ""); $count_rate = round($count / $handing_count, 4) * 100; $has_done += handing::getCountById( $value['id'], "handing_if_complete"); $has_done_rate = round($has_done / $count, 4) * 100; $processing += handing::getCountById( $value['id'], "processing"); $type_array[] = array("type0"=>$value["name"],"type1"=>$count,"type2"=>$count_rate,"type3"=>$has_done, "type4"=>$has_done_rate,"type6"=>$processing);//拼接成一个一维数组 }
其中$department_level1是这样的一个数组:
Array (
[0] => Array ( [id] => 298 [name] => 皇城 [level] => 1 ) [1] => Array ( [id] => 282 [name] => 人力资源和社会保障局 [level] => 1 ) [2] => Array ( [id] => 277 [name] => 中小企业局 [level] => 1 ) [3] => Array ( [id] => 251 [name] => 热电燃气集团 [level] => 1 ) [4] => Array ( [id] => 250 [name] => 公路管理局 [level] => 1 ) [5] => Array ( [id] => 249 [name] => 组织部 [level] => 1) [6] => Array ( [id] => 248 [name] => 经济开发区1 [level] => 1 ) [7] => Array ( [id] => 242 [name] => 俚岛镇 [level] => 1)
)
经过循环,拼接后的$type_array[]是这样的:
Array (
[0] => Array ( [type0] => 皇城 [type1] => 0 [type2] => 0 [type3] => 0 [type4] => 0 [type6] => 0 )
[1] => Array ( [type0] => 人力资源和社会保障局 [type1] => 0 [type2] => 0 [type3] => 0 [type4] => 0 [type6] => 0 )
[2] => Array ( [type0] => 中小企业局 [type1] => 0 [type2] => 0 [type3] => 0 [type4] => 0 [type6] => 0 )
[3] => Array ( [type0] => 热电燃气集团 [type1] => 1 [type2] => 1.89 [type3] => 1 [type4] => 100 [type6] => 0 )
[4] => Array ( [type0] => 公路管理局 [type1] => 2 [type2] => 3.77 [type3] => 1 [type4] => 50 [type6] => 1 )
[5] => Array ( [type0] => 组织部 [type1] => 2 [type2] => 3.77 [type3] => 1 [type4] => 50 [type6] => 1 )
[6] => Array ( [type0] => 经济开发区1 [type1] => 3 [type2] => 5.66 [type3] => 2 [type4] => 66.67 [type6] => 1 )
[7] => Array ( [type0] => 俚岛镇 [type1] => 3 [type2] => 5.66 [type3] => 2 [type4] => 66.67 [type6] => 1 )
)
之后,在foreach外部,使用这两个函数
$column = array_column($type_array,"type".$attribute);
$type_array = array_multisort($column,SORT_DESC ,$type_array);//降序排序
其中$attribute是通过页面$_GET["attribute"]获取的一个具体的数值(即:1,2,3,4,6)
如果$attribute=1,则按照“type1”索引进行排序,$column 的结果是:
Array (
[0] => 3 [1] => 3 [2] => 2 [3] => 2 [4] => 1 [5] => 0 [6] => 0 [7] => 0
)
排序后的$type_array 变为
Array (
[0] => Array ( [type0] => 俚岛镇 [type1] => 3 [type2] => 5.66 [type3] => 2 [type4] => 66.67 [type6] => 1 )
[1] => Array ( [type0] => 经济开发区1 [type1] => 3 [type2] => 5.66 [type3] => 2 [type4] => 66.67 [type6] => 1 )
[2] => Array ( [type0] => 公路管理局 [type1] => 2 [type2] => 3.77 [type3] => 1 [type4] => 50 [type6] => 1 )
[3] => Array ( [type0] => 组织部 [type1] => 2 [type2] => 3.77 [type3] => 1 [type4] => 50 [type6] => 1 )
[4] => Array ( [type0] => 热电燃气集团 [type1] => 1 [type2] => 1.89 [type3] => 1 [type4] => 100 [type6] => 0 )
[5] => Array ( [type0] => 中小企业局 [type1] => 0 [type2] => 0 [type3] => 0 [type4] => 0 [type6] => 0 )
[6] => Array ( [type0] => 人力资源和社会保障局 [type1] => 0 [type2] => 0 [type3] => 0 [type4] => 0 [type6] => 0 )
[7] => Array ( [type0] => 皇城 [type1] => 0 [type2] => 0 [type3] => 0 [type4] => 0 [type6] => 0 )
)
这样,就完成了对二维数组按照第二维的某个元素排序。