1.对数组进行某一键值的排序
function array_sort($arr,$keys,$type = 'asc'){ if (empty($arr)) return $arr; $keysvale = $newq_array = array(); foreach($arr as $k=>$v){ $keysvale[$k] = $v[$keys]; } if ($type == 'asc'){ asort($keysvale); }else{ arsort($keysvale); } reset($keysvale); foreach($keysvale as $k=>$v){ $newq_array[$k] = $arr[$k]; } return $newq_array; }
2.根据数组字符串长度冒泡排序
//数组冒泡排序 function bubble_sort_by_length($array){ $count = count($array); if ($count <= 0){ return $array; } for($i = 0;$i < $count;$i++){ for($k = $count-1;$k > $i;$k--){ if(strlen($array[$k]) > strlen($array[$k-1])){ $tmp = $array[$k]; $array[$k] = $array[$k-1]; $array[$k-1] =$tmp; } } } return $array; }
3.根据数组里面的两个key来进行冒泡排序
//数组冒泡排序 function bubble_sort_by_key($array,$key,$key_2,$type='asc',$type_2='asc'){ $count = count($array); if ($count <= 0 || empty($array)){ return array(); } if ($type == 'asc') { for ($i = 0; $i < $count; $i++) { for ($k = $count - 1; $k > $i; $k--) { if ($array[$k][$key] < $array[$k - 1][$key]) { $tmp = $array[$k]; $array[$k] = $array[$k - 1]; $array[$k - 1] = $tmp; } if ($array[$k][$key] == $array[$k - 1][$key]) { if ($type_2 == 'asc') { if ($array[$k][$key_2] < $array[$k - 1][$key_2]) { $tmp = $array[$k]; $array[$k] = $array[$k - 1]; $array[$k - 1] = $tmp; } }else{ if ($array[$k][$key_2] > $array[$k - 1][$key_2]) { $tmp = $array[$k]; $array[$k] = $array[$k - 1]; $array[$k - 1] = $tmp; } } } } } }else{ for ($i = 0; $i < $count; $i++) { for ($k = $count - 1; $k > $i; $k--) { if ($array[$k][$key] > $array[$k - 1][$key]) { $tmp = $array[$k]; $array[$k] = $array[$k - 1]; $array[$k - 1] = $tmp; } if ($array[$k][$key] == $array[$k - 1][$key]) { if ($type_2 == 'asc') { if ($array[$k][$key_2] < $array[$k - 1][$key_2]) { $tmp = $array[$k]; $array[$k] = $array[$k - 1]; $array[$k - 1] = $tmp; } }else{ if ($array[$k][$key_2] > $array[$k - 1][$key_2]) { $tmp = $array[$k]; $array[$k] = $array[$k - 1]; $array[$k - 1] = $tmp; } } } } } } return $array; }