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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP asort() Function Usage and Example

PHP Array Function Manual

The asort() function sorts the array and maintains the index relationship

Syntax

asort(array &$array[, int $sort_flags = SORT_REGULAR]);

Definition and Usage

 This function sorts the array and keeps the index associated with the unit. It is mainly used to sort combined arrays where the order of the units is very important.

Return Value

 Returns TRUE on success, or FALSE on failure.

Parameter

NumberParameters and Description
1

array (required)

It specifies an array.

2

sort_flags (optional)

You can change the sorting method using the optional parameter sort_flags.

Online Example

The array fruits is sorted in alphabetical order, and the relationship between the index and the unit remains unchanged.

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>
Test to see‹/›

Output Result:

c = apple
b = banana
d = lemon
a = orange

  PHP Array Function Manual