Name

arsort()

Synopsis

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

The arsort() function takes an array as its only parameter, and reverse sorts it by its values while preserving the keys. This is the opposite of the asort() . For example:

    $capitalcities['England'] = 'London';
    $capitalcities['Wales'] = 'Cardiff';
    $capitalcities['Scotland'] = 'Edinburgh';
    arsort($capitalcities);
    // reverse-sorted by value, so London, Edinburgh, Cardiff

Note that arsort() 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.