arsort() function in PHP


The arsort() function sorts an array in reverse order and maintain index association.

Syntax

arsort(arr, compare)

Parameters

  • arr − The specified array.

  • compare − Specifies how to compare the array elements/items. Possible values −

    • SORT_STRING - Compare items as strings

    • SORT_REGULAR - Compare items without changing types

    • SORT_NUMERIC - Compare items numerically

    • SORT_LOCALE_STRING - Compare items as strings, based on current local.

    • SORT_NATURAL - Compare items as strings using natural ordering

Return

The arsort() function returns TRUE on success and FALSE on failure.

Example

The following is an example −

 Live Demo

<?php
$rank = array("Australia"=>2,"India"=>5,"Bangladesh"=>9);
arsort($rank);

foreach($rank as $akey=>$avalue) {
   echo "Key=" . $akey . " : Value=" . $avalue;
   echo "<br>";
}
?>

Output

The following is the output −

Key=Bangladesh : Value=9
Key=India : Value=5
Key=Australia : Value=2

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 24-Jun-2020

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements