Name

asort()

Synopsis

    bool arsort ( array &arr [, int options] )

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, London

Note 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.

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.