ZetCode

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.

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.

Sorting with Different Flags

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.

Sorting Multidimensional Arrays

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.

Sorting Objects by Property

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.

Case-Insensitive Sorting

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.

Best Practices

Source

PHP arsort Documentation

This tutorial covered the PHP arsort function with practical examples showing its usage for various sorting scenarios.

Author

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.