Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Sort Array in Descending Order – rsort()

Sort Array in Descending Order – rsort()

Mastering Array Organization: Descending with rsort()


When working with arrays in PHP, arranging elements in a particular order is often essential for data analysis, presentation, or decision-making. The rsort function offers a powerful tool to achieve descending order, ensuring your data is structured effectively.

Understanding rsort()

  • Purpose: Sorts an array in descending order based on its values.
  • Syntax: rsort(array, sorting_type)
  • Parameters:
    • array: The array to be sorted.
    • sorting_type (optional): A constant specifying the sorting comparison method (default is SORT_REGULAR).
  • Return Value: Returns TRUE on success, FALSE on failure.

Key Points:

  • Modifies the Original Array: rsort() directly alters the input array, rather than creating a new sorted copy.
  • Key Reassignment: Existing keys are discarded and replaced with new numeric keys starting from 0.
  • Sorting Types: Available sorting types include:
    • SORT_REGULAR (default) for standard sorting.
    • SORT_NUMERIC for numerical comparisons.
    • SORT_STRING for string comparisons.
    • SORT_LOCALE_STRING for locale-aware string comparisons.
    • SORT_NATURAL for “natural” sorting of text and numbers.
    • SORT_FLAG_CASE for case-insensitive sorting.


$numbers = array(4, 22, 11, 6, 2);


Array ( [0] => 22 [1] => 11 [2] => 6 [3] => 4 [4] => 2 )

Additional Considerations:

  • Ascending Order: For ascending order, use the sort() function.
  • Preserving Key Associations: To maintain key associations while sorting in descending order, use arsort().
  • Sorting by Keys: To sort by keys in descending order, use krsort().


The rsort() function provides a straightforward and efficient way to arrange array elements in descending order, enhancing data organization and analysis within your PHP applications. By understanding its features and nuances, you can effectively manipulate arrays to suit your specific needs.

Scroll to Top