English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP sort() function usage and example

PHP Array Function Manual

sort() function sorts the array

Syntax

sort( $array [, $sort_flags] );

Definition and usage

This function sorts the array. After this function is executed, the elements will be arranged from lowest to highest.

Parameter

NumberParameters and descriptions
1

array(Required)

It specifies an array.

2

sort_flags(optional)

It specifies how to sort the array values. Possible values-

  • SORT_REGULAR - Default value. Compare elements normally (do not change type)

  • SORT_NUMERIC - Elements are compared as numbers

  • SORT_STRING - Elements are compared as strings

  • SORT_LOCALE_STRING - Compares elements as strings based on the current locale (locale) setting. You can change it with setlocale().

  • SORT_NATURAL - Similar to natsort() sorts each element in a 'natural order'. PHP 5.4Added in version .0.

  • SORT_FLAG_CASE - Can be combined with SORT_STRING or SORT_NATURAL (OR bitwise operation), case-insensitive string sorting.

Return value

Returns TRUE on success, FALSE on failure.

Online example

<?php
   $input = array("d"=>"limão", "a"=>"laranja", "b"=>"banana");
   sort($input);
   print_r($input);
?>
Teste veja‹/›

Output result:

Array (
   [0] => banana
   [1=> limão
   [2=> laranja
)

PHP Array Function Manual