October 2005
Intermediate to advanced
372 pages
11h 35m
English
asort()
bool arsort ( array&arr[, intoptions] )
The asort() function takes an array as its only parameter, and sorts it by its values while preserving the keys. For example:
$capitalcities['England'] = 'London';
$capitalcities['Wales'] = 'Cardiff';
$capitalcities['Scotland'] = 'Edinburgh';
asort($capitalcities);
// sorted by value, so Cardiff, Edinburgh, LondonNote that asort() works by reference, directly changing the value you pass in. The return value is either true or false, depending on whether the sorting was successful.
By default, the sort functions sort so that 2 comes before 10. You can change this using the second parameter—see the
ksort()
reference for how to do this.