sort() rsort() 此函数为 array 中的元素赋与新的键名。
这将删除原有的键名(包括关联数组),而不是仅仅将键名重新排序。

$data = array(5,8,1,7,2); //声明一个数组$data, 存放5个整数元素sort($data); //利用sort()函数将数组$data中的元素值按照由小到大顺序进行排序print_r($data); //输出:Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 7 [4] => 8 )rsort($data); //利用rsort()函数将数组$data按照由大到小的顺序对元素的值进行排序print_r($data); //输出:Array ( [0] => 8 [1] => 7 [2] => 5 [3] => 2 [4] => 1 )

把稳:关联数组也会重修索引

2. 根据键名对数组排序(按键名排序时会保留键名)

php数组倒序排列PHP 数组排序函数 AJAX

ksort() krsort()

$data = array(5=>"five",8=>"eight",1=>"one",7=>"seven",2=>"two"); //声明一个键值混乱的数组ksort($data); //利用ksort()函数按照键名对数组$data进行由小到大的排序print_r($data); //输出:Array ( [1] => one [2] => two [5] => five [7] => seven [8] => eight )krsort($data); //利用krsort()函数按照键名对数组$data进行由大到小的排序print_r($data); //输出:Array ( [8] => eight [7] => seven [5] => five [2] => two [1] => one )

3. 根据元素的值对数组排序,但原始键名保留

asort() arsort()

$data = array("l"=>"Linux", "a"=>"Apache","m"=>"MySQL","p"=>"PHP");asort($data); //利用asort()函数将数组$data按元素的值升序排序,并保留原有的键名和值print_r($data); //输出:Array ( [a] => Apache [l] => Linux [m] => MySQL [p] => PHP )arsort($data); //利用arsort()函数将数组$data按元素的值降序排序,并保留原有的键名和值print_r($data); //输出:Array ( [p] => PHP [m] => MySQL [l] => Linux [a] => Apache )rsort($data); //利用asort()函数将数组$data按元素的值降序排序,但原始键名被忽略print_r($data); //输出:Array ( [0] => PHP [1] => MySQL [2] => Linux [3] => Apache )

4. 根据"自然数排序"法对数组排序

natsort() natcasesort()

$data = array("file1.txt", "file11.txt", "File2.txt", "FILE12.txt", "file.txt");natsort($data); //普通的“自然排序”print_r($data); //输出排序后的结果,数组中包括大小写,输出不是精确的排序结果//Array ( [3] => FILE12.txt [2] => File2.txt [4] => file.txt [0] => file1.txt [1] => file11.txt )natcasesort($data); //忽略大小写的“自然排序”print_r($data); //输出“自然排序”后的结果,正常结果//Array ( [4] => file.txt [0] => file1.txt [2] => File2.txt [1] => file11.txt [3] => FILE12.txt )

自然法排序规则:大写 短者排在前面 1-9 a-z

5. 根据用户自定义规则对数组排序

usort() uasort() uksort()

$lamp = array("Linux", "Apache", "MySQL", "PHP"); //声明一个数组,个中元素值的长度不相同usort($lamp, "sortByLen"); //利用usort()函数传入用户自定义的回调函数进行数组排序print_r($lamp); // 排序后输出:Array ( [0] => PHP [1] => MySQL [2] => Linux [3] => Apache )function sortByLen($one, $two) { //自定义的函数做为回调用函数供应给usort()函数利用if (strlen($one) == strlen($two)) //如果两个参数长度相等返回0,在数组中位置不变return 0;else //第一个参数大于第二个参数返回大于0的数,否则返回小于0的数return (strlen($one) > strlen($two)) ? 1 : -1;}

6. 随机排序数组

shuffle ( array &$array ) : bool

<?php$my_array = array("red","green","blue","yellow","purple");shuffle($my_array);print_r($my_array);?>

把稳: shufffle() 不适宜关联数组, 会修正索引名称;

编写自定义函数:

function shuffle_assoc(&$array) {$keys = array_keys($array);shuffle($keys);foreach($keys as $key) {$new[$key] = $array[$key];}$array = $new;return true;}

7. 多维数组的排序

array_multisort

r 逆序 a 保持索引 u 用户自定义规则 k 键值排序

bool array_multisort ( array $ar1 [, mixed $arg [, mixed $... [, array $... ]]] )

实例1:

<?php$ar = array(array( "10" , 11 , 100 , 100 , "a" ),array( 1 , 2 , "2" , 3 , 1 ));array_multisort($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC );var_dump ( $ar );?>

实例2:

$data[] = array('volume' => 67, 'edition' => 2);$data[] = array('volume' => 86, 'edition' => 1);$data[] = array('volume' => 85, 'edition' => 6);$data[] = array('volume' => 98, 'edition' => 2);$data[] = array('volume' => 86, 'edition' => 6);$data[] = array('volume' => 67, 'edition' => 7);foreach ($data as $key => $row) {$volume[$key] = $row['volume'];$edition[$key] = $row['edition'];}array_multisort($data,$volume,$edition);echo "<pre>";print_r($data);echo "</pre>";// 将数据根据 volume 降序排列,根据 edition 升序排列// 把 $data 作为末了一个参数,以通用键排序

实例3:

array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

<?php$data = array( //声明一个$data数组,仿照了一个行和列数组array("id" => 1, "soft" => "Linux", "rating" => 3),array("id" => 2, "soft" => "Apache", "rating" => 1),array("id" => 3, "soft" => "MySQL", "rating" => 4),array("id" => 4, "soft" => "PHP", "rating" => 2),);//利用foreach遍历创建两个数组作为array_multisort的参数foreach ($data as $key => $value) {$soft[$key] = $value["soft"]; //将$data中的每个数组元素中键值为soft的值形成数组$soft$rating[$key] = $value["rating"]; //将每个数组元素中键值为rating的值形成数组$rating}array_multisort($rating, $soft, $data); //利用array_multisort()函数传入三个数组进行排序print_r($data); //输出排序后的二维数组

显示结果:

Array([0] => Array([id] => 2[soft] => Apache[rating] => 1)[1] => Array([id] => 4[soft] => PHP[rating] => 2)[2] => Array([id] => 1[soft] => Linux[rating] => 3)[3] => Array([id] => 3[soft] => MySQL[rating] => 4))

把稳: 数组排序参数是按照引用通报参数,因此会修正原数组的顺序;

编写自定义函数对二维数组进行排序

/ 对二维数组进行排序 @param array $arr 数组 @param string $keys 键名 @param string $type 类型 asc 正序 desc 倒序 @return type 返回排序后的数组(原数组未受到影响)/function array_sort($arr,$keys,$type='asc'){ //二维数组排序$keysvalue = $new_array = array();foreach ($arr as $k=>$v){$keysvalue[$k] = $v[$keys];}if($type == 'asc'){asort($keysvalue);}else{arsort($keysvalue);}reset($keysvalue);foreach ($keysvalue as $k=>$v){$new_array[$k] = $arr[$k];}return $new_array; //原数组没有受到影响}$data = array(array("id"=>1, "name"=>"zhangsan", "age"=>30),array("id"=>2, "name"=>"lisi", "age"=>28),array("id"=>3, "name"=>"wangwu", "age"=>0.1),);//利用方法echo "<pre>";print_r(array_sort($data, "age"));echo "</pre>";

显示结果

Array([2] => Array([id] => 3[name] => lisi[age] => 0.1)[1] => Array([id] => 2[name] => zhangsan[age] => 28)[0] => Array([id] => 1[name] => wangwu[age] => 30))

利用array_multisort()系统函数与自定义函数array_sort()函数的差异?

array_sort()将天生键名, 利用json_encode()函数无法解析成json数组工具([{},{},{}])。

办理方法: 利用array_values函数获取数组的所有值。

<?phpfunction array_sort($arr,$keys,$type='asc'){ //二维数组排序 $keysvalue = $new_array = array(); foreach ($arr as $k=>$v){ $keysvalue[$k] = $v[$keys]; } if($type == 'asc'){ asort($keysvalue); }else{ arsort($keysvalue); } reset($keysvalue); foreach ($keysvalue as $k=>$v){ $new_array[$k] = $arr[$k]; } return $new_array; //原数组没有受到影响}$data = array(array("id"=>1, "name"=>"zhangsan", "age"=>30),array("id"=>2, "name"=>"lisi", "age"=>28),array("id"=>3, "name"=>"wangwu", "age"=>0.1),);//利用方法echo "<pre>";print_r(array_values(array_sort($data, "age")));echo "</pre>";

显示结果:

Array([0] => Array([id] => 3[name] => zhangsan[age] => 0.1)[1] => Array([id] => 2[name] => lisi[age] => 28)[2] => Array([id] => 1[name] => wangwu[age] => 30))