Latest web development tutorials

PHP sort () function

PHP Array Reference Complete PHP Array Reference

Examples

$ Cars array elements in alphabetical ascending order:

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

Running instance »

Definition and Usage

sort () function array of numeric values ​​in descending order.

Tip: Use rsort () function array of numeric values in descending order.

grammar

sort( array,sortingtype );

参数 描述
array 必需。规定要进行排序的数组。
sortingtype 可选。规定如何排列数组的元素/项目。可能的值:
  • 0 = SORT_REGULAR - 默认。把每一项按常规顺序排列(Standard ASCII,不改变类型)。
  • 1 = SORT_NUMERIC - 把每一项作为数字来处理。
  • 2 = SORT_STRING - 把每一项作为字符串来处理。
  • 3 = SORT_LOCALE_STRING - 把每一项作为字符串来处理,基于当前区域设置(可通过 setlocale() 进行更改)。
  • 4 = SORT_NATURAL - 把每一项作为字符串来处理,使用类似 natsort() 的自然排序。
  • 5 = SORT_FLAG_CASE - 可以结合(按位或)SORT_STRING 或 SORT_NATURAL 对字符串进行排序,不区分大小写。

technical details

return value: If successful it returns TRUE, on failure returns FALSE.
PHP version: 4+


More examples

Example 1

$ Numbers array elements in ascending order by numbers:

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

Running instance »


PHP Array Reference Complete PHP Array Reference