A. 按照单个key的值来进行排序(针对二维数组)
public function sortArr($arr, $field, $sort = 'SORT_ASC'){
if(!$field) return $arr;
$fieldArr = array();
foreach($arr as $key => $value){
$fieldArr['field_'.$key] = $value[$field];
}
if($sort == 'SORT_ASC'){
asort($fieldArr, 1);
}else{
arsort($fieldArr, 1);
}
$result = array();
foreach ($fieldArr as $field_key => $value) {
$field = explode('_', $field_key);
$result[] = $arr[$field[1]];
}
return $result;
}
B. 按照两个key的值来进行排序(针对二维数组)
public function keysSort($fieldArr, $arr1, $arr2){
$field1 = $fieldArr[0];
$field2 = $fieldArr[1];
if($arr1[$field1] == $arr2[$field1]){
return $arr1[$field2] > $arr2[$field2];
}else{
return $arr1[$field1] > $arr2[$field1];
}
}
public function sortArrByKeys($arr, $fieldArr){
for($i=0;$i<count($arr);$i++){
$cmpObj = $arr[$i];
for($j=$i+1;$j<count($arr);$j++){
$flag = $this->keysSort($fieldArr, $cmpObj, $arr[$j]);
if($flag){
$arr[$i] = $arr[$j];
$arr[$j] = $cmpObj;
$cmpObj = $arr[$i];
}
}
}
return $arr;
}