$test_array = array (12, 5, 10, 9, 100, - 10 ); //$sort_array = sortasc ( $test_array ); $sort_array = sortdesc ( $test_array ); print_r ( $sort_array ); //倒序 function sortdesc($array) { $count = count ( $array ); if ($count <= 0) return; for($i = 0; $i < $count; $i ++) { //倒序比较 for($j = $count - 1; $j > $i; $j --) { if ($array [$j - 1] > $array [$j]) { //用临时变量赋值 $temp = $array [$j]; $array [$j] = $array [$j - 1]; $array [$j - 1] = $temp; } } } return $array; } //顺序 function sortasc($array) { $count = count ( $array ); if ($count <= 0) return; for($i = 0; $i < $count; $i ++) { //顺序比较 for($j = 0; $j < $count - $i - 1; $j ++) { if ($array [$j + 1] < $array [$j]) { //交换变量不赋中间值 $array [$j] = $array [$j + 1] + $array [$j]; $array [$j + 1] = $array [$j] - $array [$j + 1]; $array [$j] = $array [$j] - $array [$j + 1]; } } } return $array; }