arsort() Function
PHP中的arsort()用于根据值对数组进行排序。它的排序方式是保持独立与价值之间的关系。默认情况下,它按照值的降序排序。
语法
1 | bool arsort( $array, $sorting_type ) |
参数:此函数接受上述两个参数,如下所述:
- $array:这个参数指定要排序的数组。它是一个强制参数。
- $sorting_type:该参数指定用户定义函数的名称,该函数将用于对数组$array的键进行排序。这个比较函数必须返回一个整数。
返回值:成功时返回True,失败时返回False。
下面的程序演示了PHP中的arsort()函数。
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 | <?php // PHP program to ilustrate // arsort() function // Input differet array elements $arr = array("0" => "Gsky8g", "1" => "Practice", "2" => "Contribute", "3" => "Java", "4" => "Videos", "5" => "Report Bug", "6" => "Article", "7" => "Sudo Placement" ); // Implementation of arsort() arsort($arr); // for-Loop for displaying result foreach ($arr as $key => $val) { echo "[$key] = $val"; echo"\n"; } ?> |
输出
1 2 3 4 5 6 7 8 | [4] = Videos [7] = Sudo Placement [5] = Report Bug [1] = Practice [3] = Java [0] = Gsky8g [2] = Contribute [6] = Article |
程序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 | <?php // PHP program to ilustrate // arsort() function // Input differet array elements $arr = array("a" => 11, "b" => 22, "d" => 33, "n" => 44, "o" => 55, "p" => 66, "p" => 77, "q" => 88, ); // Implementation of arsort() arsort($arr); // for-Loop for displaying result foreach ($arr as $key => $val) { echo "[$key] = $val"; echo"\n"; } ?> |
输出
1 2 3 4 5 6 7 | [q] = 88 [p] = 77 [o] = 55 [n] = 44 [d] = 33 [b] = 22 [a] = 11 |
相关文章
参考