PHP sort() 函数

实例

对数组 $cars 中的元素按字母进行升序排序:

<?php
$cars=array("Volvo","BMW","Toyota");
sort($cars);
?>

Run Example

定义和用法

sort() 函数对索引数组进行升序排序。

注释:本函数为数组中的单元赋予新的键名。原有的键名将被删除。

Returns TRUE if successful, otherwise FALSE.

Tip:Use rsort() The function sorts the index array in descending order.

Syntax

sort(array,sortingtype);
Parameters Description
array Required. Specify the array to be sorted.
sortingtype

Optional. Specify how to compare array elements/items. Possible values:

  • 0 = SORT_REGULAR - Default. Arrange each item in the regular order (Standard ASCII, do not change type)
  • 1 = SORT_NUMERIC - Treat each item as a number.
  • 2 = SORT_STRING - Treat each item as a string.
  • 3 = SORT_LOCALE_STRING - Treat each item as a string, based on the current locale (can be changed by setlocale()).
  • 4 = SORT_NATURAL - Treat each item as a string, using a natural sorting similar to natsort().
  • 5 = SORT_FLAG_CASE - Can combine (bitwise OR) SORT_STRING or SORT_NATURAL to sort strings without case sensitivity.

Technical Details

Return Value: Returns TRUE if successful, FALSE otherwise.
PHP Version: 4+

More Examples

Example 1

Sort elements of the array $numbers in ascending order by number:

<?php
$numbers=array(4,6,2,22,11);
sort($numbers);
?>

Run Example