PHP arsort function tutorial shows how to sort associative arrays in descending order in PHP. Learn arsort with practical examples.
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.
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.
This demonstrates sorting an associative array by values in descending order.
basic_arsort.php
<?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.
arsort supports different sorting flags for varied behavior.
sorting_flags.php
<?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.
When working with complex data, we can combine arsort with other functions for multidimensional sorting.
multidimensional.php
<?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.
We can sort an array of objects by one of their properties using arsort.
object_sorting.php
<?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.
For string values, we might want case-insensitive sorting using a custom approach.
case_insensitive.php
<?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.
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.
This tutorial covered the PHP arsort function with practical examples showing its usage for various sorting scenarios.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.