这个 排序() 函数是PHP中的一个内置函数,用于按升序对数组进行排序,即从小到大。它对实际数组进行排序,因此更改会反映在原始数组中。该函数为我们提供了6种排序类型,根据这些类型可以对数组进行排序。
null
语法:
bool sort($array, sorting_type)
参数:
- $array- 该参数指定要排序的数组。这是一个强制性参数
- 分类类型– 这是一个可选参数。有6种分拣类型,如下所述:
- 按常规排序 –当我们经过时 0 或 按常规排序 在 分类类型 参数时,数组中的项将正常比较。
- 数字排序 –当我们经过时 1. 或 数字排序 在 分类类型 参数时,数组中的项将进行数值比较
- 排序字符串 –当我们经过时 2. 或 排序字符串 在 分类类型 参数,数组中的项按字符串进行比较
- 排序\u区域设置\u字符串 –当我们经过时 3. 或 排序\u区域设置\u字符串 在 分类类型 参数,数组中的项将根据当前区域设置作为字符串进行比较
- 自然排序 –当我们经过时 4. 或 自然排序 在 分类类型 参数,数组中的项将使用自然顺序作为字符串进行比较
- 分拣标志箱 –当我们经过时 5. 或 分拣标志箱 在 分类类型 参数,数组中的项将作为字符串进行比较。这些项目被视为不区分大小写,然后进行比较。它可以使用|(按位运算符)与 自然排序 或 排序字符串 .
返回值: 它返回一个布尔值,成功时为TRUE,失败时为False。它按升序对原始数组进行排序,并将其作为参数传递。
例如:
Input : $array = [3, 4, 1, 2] Output : Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) Input : $array = ["geeks2", "raj1", "striver3", "coding4"] Output : Array ( [0] => coding4 [1] => geeks2 [2] => raj1 [3] => striver3 )
下面的程序演示了PHP中的sort()函数:
项目1: 程序演示sort()函数的使用。
<?php // PHP program to demonstrate the use of sort() function $array = array (3, 4, 2, 1); // sort function sort( $array ); // prints the sorted array print_r( $array ); ?> |
输出:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
项目2: 程序演示如何使用sort()函数对字符串大小写进行敏感排序。
<?php // PHP program to demonstrate the use of sort() function // sorts the string case-sensitively $array = array ( "geeks" , "Raj" , "striver" , "coding" , "RAj" ); // sort function, sorts the string case-sensitively sort( $array , SORT_STRING); // prints the sorted array print_r( $array ); ?> |
输出:
Array ( [0] => RAj [1] => Raj [2] => coding [3] => geeks [4] => striver )
方案3: 程序演示如何使用sort()函数对字符串大小写进行不敏感排序。
<?php // PHP program to demonstrate the use // of sort() function sorts the string // case-insensitively $array = array ( "geeks" , "Raj" , "striver" , "coding" , "RAj" ); // sort function, sorts the // string case-insensitively sort( $array , SORT_STRING | SORT_FLAG_CASE); // prints the sorted array print_r( $array ); ?> |
输出:
Array ( [0] => coding [1] => geeks [2] => Raj [3] => RAj [4] => striver )
参考 : http://php.net/manual/en/function.sort.php
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END