PHP » Arrays » asort()

Syntax:
bool asort (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 ascending order preserving keys.

asort() sorts an array in ascending order, but unlike sort(), the previous keys are not discarded. 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");

asort($names);
asort($array1);

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

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

asort() reorders the elements in an array without changing the keys.

See Also: