array_multisort 多维数组排序函数
参数中的数组被当成一个表的列并以行来进行排序 - 这类似 sql 的 order by 子句的功能,第一个数组是要排序的主要数组,数组中的行(值)比较为相同的话,就会按照下一个输入数组中相应值的大小进行排序,依此类推,PHP实例代码如下:
- <?php
- $a1=array("dog","cat");
- $a2=array("fido","missy");
- array_multisort($a1,$a2);
- print_r($a1);
- print_r($a2);
- ?>
- //输出:
- array ( [0] => cat [1] => dog )
- array ( [0] => missy [1] => fido )
PHP实例,名次排列,代码如下:
- <?php
- $grade = array("score" => array(70, 95, 70.0, 60, "70"),
- "name" => array("zhang san", "li si", "wang wu",
- "zhao liu", "liu qi"));
- array_multisort($grade["score"], sort_numeric, sort_desc,
- // 将分数作为数值,由高到低排序
- $grade["name"], sort_string, sort_asc);
- // 将名字作为字符串,由小到大排序
- var_dump($grade);
- ?> //开源软件:phpfensi.com
- //上例将输出:
- array(2) {
- ["score"]=>
- array(5) {
- [0]=>
- int(95)
- [1]=>
- string(2) "70"
- [2]=>
- float(70)
- [3]=>
- int(70)
- [4]=>
- int(60)
- }
- ["name"]=>
- array(5) {
- [0]=>
- string(5) "li si"
- [1]=>
- string(6) "liu qi"
- [2]=>
- string(7) "wang wu"
- [3]=>
- string(9) "zhang san"
- [4]=>
- string(8) "zhao liu"
- }
- }
多维数组排序,代码如下:
- <?php
- $arr = array (array (1, 2, 3, 4), array ("a", "b", "b", "d"));
- array_multisort($arr[0], sort_desc, $arr[1], sort_asc);
- print_r($arr);
- ?>
- //运行该例子输出:
- array
- (
- [0] => array
- (
- [0] => 4
- [1] => 2
- [2] => 2
- [3] => 1
- )
- [1] => array
- (
- [0] => d
- [1] => b
- [2] => c
- [3] => a
- )
- )
array_multisort() 函数对多个数组或多维数组进行排序.