PHP arsort Function
last modified March 13, 2025
The PHP arsort
function sorts an associative array in descending
order according to the value while maintaining key-value associations.
Basic Definition
The arsort
function sorts an array by values in descending order.
Unlike rsort
, it preserves the key-value pairs of the array.
Syntax: arsort(array &$array, int $flags = SORT_REGULAR): bool
.
The function modifies the original array and returns true on success.
Basic arsort Example
This demonstrates sorting an associative array by values in descending order.
<?php $fruits = [ "apple" => 5, "banana" => 2, "orange" => 8, "pear" => 3 ]; arsort($fruits); print_r($fruits);
Output shows the fruits sorted by quantity in descending order. The key-value pairs remain intact, only their order changes.
Sorting with Different Flags
arsort
supports different sorting flags for varied behavior.
<?php $numbers = [ "first" => "10", "second" => "2", "third" => "100", "fourth" => "20" ]; arsort($numbers, SORT_STRING); echo "String sort:\n"; print_r($numbers); arsort($numbers, SORT_NUMERIC); echo "\nNumeric sort:\n"; print_r($numbers);
The first sort treats values as strings (100 comes before 20), while the second does numeric comparison. Flags change how values are compared.
Sorting Multidimensional Arrays
When working with complex data, we can combine arsort
with other
functions for multidimensional sorting.
<?php $students = [ ["name" => "Alice", "score" => 85], ["name" => "Bob", "score" => 92], ["name" => "Charlie", "score" => 78] ]; $scores = array_column($students, "score"); arsort($scores); $sortedStudents = []; foreach ($scores as $key => $value) { $sortedStudents[] = $students[$key]; } print_r($sortedStudents);
This sorts students by score in descending order while maintaining the full student data structure. We first extract scores for sorting.
Sorting Objects by Property
We can sort an array of objects by one of their properties using arsort
.
<?php class Product { public function __construct( public string $name, public float $price ) {} } $products = [ new Product("Laptop", 999.99), new Product("Phone", 699.99), new Product("Tablet", 399.99) ]; $prices = []; foreach ($products as $key => $product) { $prices[$key] = $product->price; } arsort($prices); $sortedProducts = []; foreach ($prices as $key => $price) { $sortedProducts[] = $products[$key]; } print_r($sortedProducts);
This example sorts products by price in descending order. We first extract prices to an array, sort it, then rebuild the object array in the new order.
Case-Insensitive Sorting
For string values, we might want case-insensitive sorting using a custom approach.
<?php $words = [ "a" => "Zebra", "b" => "apple", "c" => "Banana", "d" => "orange" ]; $lowercaseValues = array_map('strtolower', $words); arsort($lowercaseValues); $sortedWords = []; foreach ($lowercaseValues as $key => $value) { $sortedWords[$key] = $words[$key]; } print_r($sortedWords);
This sorts words alphabetically in descending order ignoring case. We create a temporary array with lowercase values for case-insensitive comparison.
Best Practices
- Preserve Keys: Use
arsort
when you need to maintain key-value associations. - Sort Flags: Choose appropriate sorting flags for your data type.
- Large Arrays: For very large arrays, consider more efficient sorting algorithms.
- Complex Data: Combine with other array functions for multidimensional sorting.
Source
This tutorial covered the PHP arsort
function with practical
examples showing its usage for various sorting scenarios.
Author
List all PHP Array Functions.