PHP asort()Functions

PHP Array ReferenceComplete PHP Array Reference Manual

Examples

Sort an associative array in ascending order according to its values:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);
?>

Run Example »

Definition and Usage

The asort() function sorts an associative array in ascending order according to its values.

Tip:Please usearsort()to sort an associative array in descending order according to its values.

Tip:Please useksort()to sort an associative array in ascending order according to its keys.

Syntax

asort(array,sortingtype);

Parameter Description
array Required. Specifies the array to be sorted.
sortingtype Optional. Specifies how to arrange the elements/items of the array. Possible values:
  • 0 = SORT_REGULAR - Default. Compare items normally (Standard ASCII, without changing 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 viasetlocale()setlocale()).
  • 4 = SORT_NATURAL - Treat each item as a string, using natural ordering similar tonatsort()natsort().
  • 5 = SORT_FLAG_CASE - Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.

Technical Details

Return Value: Returns TRUE on success, FALSE on failure.
PHP Version: 4+


PHP Array ReferenceComplete PHP Array Reference Manual

Other Extensions