ksort() Function
ksort()函数是PHP中的内建函数,用于根据键值按升序排列数组。它以索引和值之间的关系得以维护的方式进行排序。
语法
1 | bool ksort( $array, $sorting_type ) |
参数:此函数接受上述两个参数,如下所述:
- $array:这个参数指定需要排序的数组。它是一个强制参数。
- $sorting_type: 这是一个可选参数。下面将讨论不同的分类类型:
- sorting_type的值是SORT_REGULAR,然后比较正常的项。
- sorting_type的值是SORT_NUMERIC,然后对项目进行数值比较。
- sorting_type的值是SORT_STRING,然后将项作为字符串进行比较。
- sorting_string: $sorting_type的值是SORT_STRING,然后根据当前语言环境将项比较为字符串。
返回值:成功时返回True,失败时返回False。
下面的程序演示了PHP中的ksort()函数。
程序1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php // PHP program to illustrate // ksort()function // Input differet array elements $arr = array("13" =>"ASP.Net", "12" =>"C#", "11" =>"Graphics", "4" =>"Video Editing", "5" =>"Photoshop", "6" =>"Article", "4" =>"Placement", "8" =>"C++", "7" =>"XML", "10" =>"Android", "1" =>"SQL", "2" =>"PL/Sql", "3" =>"End", "0" =>"Java", ); // Implementation of ksort() ksort($arr); // for-Loop for displaying result foreach ($arr as $key => $val) { echo "[$key] = $val"; echo"\n"; } ?> |
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 | [0] = Java [1] = SQL [2] = PL/Sql [3] = End [4] = Placement [5] = Photoshop [6] = Article [7] = XML [8] = C++ [10] = Android [11] = Graphics [12] = C# [13] = ASP.Net |
程序2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php // PHP program to illustrate // ksort function // Input differet array elements $arr = array("z" => 11, "y" => 22, "x" => 33, "n" => 44, "o" => 55, "b" => 66, "a" => 77, "m" => 2, "q" => -11, "i" => 3, "e" => 56, "d" => 1, ); // Implementation of ksort ksort($arr); // for-Loop for displaying result foreach ($arr as $key => $val) { echo "[$key] = $val"; echo"\n"; } ?> |
输出
1 2 3 4 5 6 7 8 9 10 11 12 | [a] = 77 [b] = 66 [d] = 1 [e] = 56 [i] = 3 [m] = 2 [n] = 44 [o] = 55 [q] = -11 [x] = 33 [y] = 22 [z] = 11 |
参考