asort() Function
sort()函数是PHP中的一个内建函数,用于根据值对数组进行排序。它的排序方式是保持索引和值之间的关系。默认情况下,它按值的升序排序。
语法
1 | bool asort( $array, $sorting_type ) |
参数:该函数接受上述两个参数,描述如下:
- $array:该参数指定要排序的数组。它是一个强制参数。
- $sorting_type:这是一个可选参数。以下讨论了不同的分类类型:
- SORT_REGULAR: $sorting_type的值为SORT_REGULAR,则项目将正常比较。
- SORT_NUMERIC: $sorting_type的值是SORT_NUMERIC,然后对项目进行数值比较。
- SORT_STRING: $sorting_type的值是SORT_STRING,然后将项目作为字符串进行比较。
- SORT_LOCALE_STRING: $sorting_type的值是SORT_STRING,然后根据当前地区将项目作为字符串进行比较。
返回值:该函数成功返回True,失败返回False。
下面的程序演示了PHP中的asort()函数。
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 | <?php // PHP program to ilustrate // asort() function // Input differet array elements $arr = array("0" => "Web Technology", "1" => "Machine Learing", "2" => "GeeksforGeeks", "3" => "Computer Graphics", "4" => "Videos", "5" => "Report Bug", "6" => "Article", "7" => "Sudo Placement", "8" => "SContribute", "9" => "Reset", "10" => "Copy", "11" => "IDE", "12" => "Gate Note", ); // Implementation of asort() asort($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 | [6] = Article [3] = Computer Graphics [10] = Copy [12] = Gate Note [2] = GeeksforGeeks [11] = IDE [1] = Machine Learing [5] = Report Bug [9] = Reset [8] = SContribute [7] = Sudo Placement [4] = Videos [0] = Web Technology |
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 | [6] = Article [3] = Computer Graphics [10] = Copy [12] = Gate Note [2] = GeeksforGeeks [11] = IDE [1] = Machine Learing [5] = Report Bug [9] = Reset [8] = SContribute [7] = Sudo Placement [4] = Videos [0] = Web 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 29 | <?php // PHP program to ilustrate // asort() 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 asort() asort($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 | [q] = -11 [z] = 1 [s] = 2 [t] = 3 [a] = 11 [b] = 22 [d] = 33 [n] = 44 [o] = 55 [p] = 66 [r] = 77 [u] = 1000 |
参考