Func Array Arsort
## PHP arsort() Function
The `arsort()` function is a built-in PHP function used to sort an associative array in descending order according to its **values**.
Crucially, this function maintains the key-value correlation during the sorting process. This makes it ideal for sorting associative arrays where the relationship between keys and values must be preserved (e.g., sorting a list of users by their high scores or ages).
---
## Syntax and Usage
### Syntax
```php
arsort(array &$array, int $flags = SORT_REGULAR): bool
```
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `array` | Array | **Required.** The input array to be sorted. Passed by reference. |
| `flags` | Integer | **Optional.** Modifies the sorting behavior using one of the predefined sorting flags. |
### Sorting Flags (`flags`)
You can customize the sorting behavior using the following flags:
* **`SORT_REGULAR`** (Default): Compare items normally (standard ASCII comparison, without changing types).
* **`SORT_NUMERIC`**: Compare items numerically.
* **`SORT_STRING`**: Compare items as strings.
* **`SORT_LOCALE_STRING`**: Compare items as strings based on the current locale. The locale can be changed using `setlocale()`.
* **`SORT_NATURAL`**: Compare items as strings using "natural ordering" (e.g., "item10" comes after "item2"), similar to `natsort()`.
* **`SORT_FLAG_CASE`**: Can be combined (using bitwise OR `|`) with `SORT_STRING` or `SORT_NATURAL` to sort strings case-insensitively.
### Return Value
* Returns `true` on success.
* Returns `false` on failure.
---
## Code Examples
### Example 1: Basic Usage (Sorting by Value in Descending Order)
The following example demonstrates how to sort an associative array of names and ages by age in descending order.
```php
"35", "Ben" => "37", "Joe" => "43");
// Sort the array by value in descending order
arsort($age);
// Display the sorted array
print_r($age);
?>
```
**Output:**
```text
Array
(
=> 43
=> 37
=> 35
)
```
---
### Example 2: Sorting with Case-Insensitive Natural Ordering
You can combine flags to achieve advanced sorting behaviors, such as case-insensitive natural sorting.
```php
"photo10.jpg",
"file2" => "Photo2.jpg",
"file3" => "photo1.jpg"
);
// Sort using natural order and case-insensitivity in descending order
arsort($files, SORT_NATURAL | SORT_FLAG_CASE);
print_r($files);
?>
```
**Output:**
```text
Array
(
=> photo10.jpg
=> Photo2.jpg
=> photo1.jpg
)
```
---
## Considerations and Related Functions
### Key Considerations
* **In-Place Sorting:** The `arsort()` function modifies the original array directly because it passes the array by reference. It does not return a new sorted array.
* **Key Preservation:** Unlike `rsort()`, which resets the array keys to numerical indexes (0, 1, 2...), `arsort()` preserves the original keys of the elements.
### Related Functions
If `arsort()` does not fit your exact use case, consider these related PHP array sorting functions:
* [`asort()`](func-array-asort.html): Sorts an associative array in **ascending** order by **value**.
* [`krsort()`](func-array-krsort.html): Sorts an associative array in **descending** order by **key**.
* [`ksort()`](func-array-ksort.html): Sorts an associative array in **ascending** order by **key**.
* [`rsort()`](func-array-rsort.html): Sorts an indexed array in **descending** order (keys are reset).
YouTip