<?php
function sortData($data, $col, $order){
if (count($data)) $temp_array[key($data)] = array_shift($data);
foreach($data as $key => $val){
$offset = 0;
$found = false;
foreach($temp_array as $tmp_key => $tmp_val){
if(!$found and strtolower($val[$col]) > strtolower($tmp_val[$col])){
$temp_array = array_merge((array)array_slice($temp_array,0,$offset),
array($key => $val),
array_slice($temp_array,$offset)
);
$found = true;
}
$offset++;
}
if(!$found) {
$temp_array = array_merge($temp_array, array($key => $val));
}
}
if (strtolower($order)=="asc"){
$array = array_reverse($temp_array);
} else {
$array = $temp_array;
}
return $array;
}
$testArray = array(
array("name"=>"平和","fenshu"=>70),
array("name"=>"阳虚","fenshu"=>65),
array("name"=>"气郁","fenshu"=>80),
array("name"=>"湿热","fenshu"=>34),
array("name"=>"血虚","fenshu"=>74)
);
$a = sortdata($testArray, "fenshu","desc");
print_r($a);
?>
PHP 多维数组排序算法
最新推荐文章于 2021-12-02 18:34:37 发布