PHP » Arrays » arsort()

Syntax:
bool arsort (array array [, int sort_type])
array
Array to be sorted.
sort_type
Can be SORT_REGULAR, SORT_NUMERIC, or SORT_STRING.

Sorts an array in descending order preserving keys.

arsort() works as asort(), but sorts in descending instead of ascending order. The previous keys are not discarded. Instead, the elements are merely reordered. The optional sort_type parameter specifies whether the elements should be sorted as strings or numerically. If the array was sorted successfully, TRUE is returned.

Examples

Code:
<pre>
<?php

$names = array("Johnson", "Williams", "Smith", "Jones");
$array1 = array(
   "strawberry" => "red",
   "blueberry" => "blue",
   "raspberry" => "red");

arsort($names);
arsort($array1);

print_r($names);
print "<br>";
print_r($array1);

?>
</pre>
Output:
Array
(
   [1] => Williams
   [2] => Smith
   [3] => Jones
   [0] => Johnson
)
Array
(
   [strawberry] => red
   [raspberry] => red
   [blueberry] => blue
)
Explanation:

arsort() reorders the elements in an array in descending order and without changing the keys.

See Also: