krsort() Function
krsort()函数是PHP中的一个内建函数,用于按键对数组按照索引值的倒序排序。它以索引和值之间的关系得以维护的方式进行排序。
语法
1 | bool krsort( $array, $sorting_type ) |
参数:此函数接受上述两个参数,如下所述:
- $array:这个参数指定要排序的数组。它是一个强制参数。
- $sorting_type:这是一个可选参数。下面将讨论不同的分类类型:
- SORT_REGULAR:的值是SORT_REGULAR,然后比较正常的项。
- SORT_NUMERIC: 的值是SORT_NUMERIC,然后对项目进行数值比较。
- SORT_STRING: 的值是SORT_STRING,然后将项比较为string。
- SORT_LOCALE_STRING: $sorting_type的值是SORT_STRING,然后根据当前语言环境将项比较为字符串。
返回值:成功时返回True,失败时返回False。
下面的程序演示了PHP中的krsort()函数。
程序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 | <?php // PHP program to ilustrate // krsort()function // Input differet array elements $arr = array("0" =>"Technology", "1" =>"Machine", "2" =>"G-sky8g", "3" =>"Graphics", "4" =>"Videos", "5" =>"Report", "6" =>"Article", "7" =>"Placement", "8" =>"Contribute", "9" =>"Reset", "10" =>"Copy", ); // Implementation of krsort() krsort($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 | [10] = Copy [9] = Reset [8] = Contribute [7] = Placement [6] = Article [5] = Report [4] = Videos [3] = Graphics [2] = G-sky8g [1] = Machine [0] = Technology |
程序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 | <?php // PHP program to ilustrate // krsort function // Input differet array elements $arr = array("a" => 11, "b" => 22, "d" => 33, "n" => 44, "o" => 55, "p" => 66, "r" => 77, "s" => 2, "q" => -11, "t" => 3, "u" => 1000, "z" => 1, ); // Implementation of krsort krsort($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 | [z] = 1 [u] = 1000 [t] = 3 [s] = 2 [r] = 77 [q] = -11 [p] = 66 [o] = 55 [n] = 44 [d] = 33 [b] = 22 [a] = 11 |
参考