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 an array while maintaining the index relationship

Syntax

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

Definition and Usage

 This function sorts the array while keeping the index relationship between the array and its elements. It is mainly used to sort combined arrays where the order of the elements 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 alphabetically, and the index relationship of the elements 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