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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP arsort() Function Usage and Example

PHP Array Function Manual

The arsort() function sorts an array in reverse order while preserving the index relationship

Syntax

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

Definition and Usage

This function sorts the array while keeping the index relationship between the array and the elements.
主要用于对那些单元顺序很重要的组合数组进行排序。

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 reverse alphabetical order, and the index relationship of the elements remains unchanged.

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

Output Result:

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

  PHP Array Function Manual