The arsort Function

The arsort function sorts an array in descending order while preserving keys. The function modifies the array itself.

Syntax

phparsort(array &$array, int $flags = SORT_REGULAR): bool

Example

Let's sort an associative array in descending order:

php<?php
  $arr = [
    'b' => 1,
    'e' => 3,
    'c' => 2,
    'a' => 5,
    'd' => 4,
  ];
  
  arsort($arr);
  var_dump($arr);
?>

Code execution result:

php[
  'd' => 4,
  'e' => 3,
  'c' => 2,
  'b' => 1,
  'a' => 5,
]

See Also

  • the sort function,
    which sorts in ascending order
  • the rsort function,
    which sorts in descending order
  • the ksort function,
    which sorts by keys in ascending order
  • the krsort function,
    which sorts by keys in descending order
  • the asort function,
    which sorts in ascending order while preserving keys
  • the arsort function,
    which sorts in descending order while preserving keys
  • the natsort function,
    which sorts using natural order algorithm
  • the natcasesort function,
    which sorts using natural order algorithm case-insensitively
  • the usort function,
    which sorts using a callback function
  • the uksort function,
    which sorts by keys using a callback function
  • the uasort function,
    which sorts using a callback function while preserving keys
  • the array_multisort function,
    which sorts multiple arrays