PHP sort() 函数
❮ PHP 数组参考
示例
按字母升序对 $cars 数组的元素进行排序:
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>
亲自试一试 »
定义和用法
sort() 函数按升序对索引数组进行排序。
提示:使用排序()函数按降序对索引数组进行排序。
语法
参数值
Parameter |
Description |
array |
Required. Specifies the array to sort |
sorttype |
Optional. Specifies how to compare the array elements/items. Possible values:
- 0 = SORT_REGULAR - Default. Compare items normally (don't change types)
- 1 = SORT_NUMERIC - Compare items numerically
- 2 = SORT_STRING - Compare items as strings
- 3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale
- 4 = SORT_NATURAL - Compare items as strings using natural ordering
- 5 = SORT_FLAG_CASE - Can be combined with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
|
技术细节
返回值: |
真的 |
PHP 版本: |
4+ |
PHP 变更日志: |
PHP 8.2.0:现在返回 TRUE(之前返回 bool) |
更多示例
示例
按数字升序对 $numbers 数组的元素进行排序:
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>
亲自试一试 »
❮ PHP 数组参考