Backend Development 5 min read

Understanding PHP's arsort() Function: Definition, Usage, Parameters, and Examples

This article explains PHP's arsort() function, detailing its definition, syntax, parameters, optional sorting flags, return value, and provides multiple code examples demonstrating how to sort associative and indexed arrays in descending order while preserving key associations.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding PHP's arsort() Function: Definition, Usage, Parameters, and Examples

PHP is a widely used programming language for web development, offering many built‑in functions; among them, arsort() is a useful function for sorting arrays by value in descending order.

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

The function takes a reference to the array to be sorted; the optional $sort_flags parameter specifies the sorting type, defaulting to SORT_REGULAR .

arsort() sorts the array values in descending order while preserving the association between keys and values.

Usage Example 1 (associative array):

<code>$array = array("apple" => 2, "banana" => 3, "orange" => 1);<br/>arsort($array);<br/>print_r($array);</code>

The array is sorted so that the element with the highest value ("banana" => 3) appears first, followed by "apple" and "orange", while the original keys remain attached to their values.

Usage Example 2 (indexed array):

<code>$array = array(2, 5, 1, 3, 4);<br/>arsort($array);<br/>print_r($array);</code>

The indexed array is reordered in descending order (5, 4, 3, 2, 1) and the original numeric keys are preserved.

The function accepts two parameters:

$array (required) – the array to be sorted, which can be associative or indexed.

$sort_flags (optional) – determines the sorting behavior. Possible constants are:

SORT_REGULAR : default, compares values after converting them to appropriate types.

SORT_NUMERIC : compares values numerically.

SORT_STRING : compares values as strings.

SORT_LOCALE_STRING : compares strings based on the current locale.

SORT_NATURAL : performs natural order sorting.

SORT_FLAG_CASE : combines SORT_STRING or SORT_NATURAL with case‑insensitive comparison.

The return value of arsort() is a boolean: true on successful sorting, false otherwise.

Note that arsort() modifies the original array in place; to preserve the original data, create a copy of the array before calling the function.

In summary, arsort() is a commonly used PHP function for descending order sorting of arrays while keeping key associations, and mastering its usage can improve data handling efficiency in backend development.

php-functionsarray-sortingarsort
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.