Sorting arrays

bool asort ( array input [, int sort_flags])

bool ksort ( array input [, int sort_flags])

bool arsort ( array input [, int sort_flags])

bool krsort ( array input [, int sort_flags])

Although there is a basic array sorting function, called simply sort(), it makes no attempt to preserve the values of your keys and so usually does more harm than good. In its place are asort() and ksort(), which are very similar- asort() sorts an array by its values, whereas ksort() sorts an array by its keys. The difference can be seen clearly in this next script:

<?php
    $capitalcities['England'] = 'London';
    $capitalcities['Wales'] = 'Cardiff';
    $capitalcities['Scotland'] = 'Edinburgh';
    ksort($capitalcities);
    var_dump($capitalcities);
    asort($capitalcities);
    var_dump($capitalcities);
?>

Note that both ksort() and asort() work by reference, directly changing the value you pass in - the return value is either true or false, depending on whether the sorting was successful. The first var_dump will output the array sorted by key, therefore it will be England, Scotland, then Wales. The second var_dump will output the array sorted by values, therefore it will be Cardiff, Edinburgh, London.

These two functions also have companions, arsort() and krsort(), which reverse sort the array and reverse sort the array by key respectively.

There is one particular situation that may catch you out when sorting arrays, and that's when you have not assigned your own keys and are accessing the data by index. Consider this next script:

<?php
    $capitalcities[] = 'London';
    $capitalcities[] = 'Cardiff';
    $capitalcities[] = 'Edinburgh';
    var_dump($capitalcities);
    asort($capitalcities);
    var_dump($capitalcities);
?>

This time the keys are being assigned by PHP, so 0 will be London, 1 will be Cardiff, and 2 will be Edinburgh. When sorted, however, the keys 0, 1, and 2 stay with the values, which means the second var_dump() call will output this:

array(3) {
    [1]=>
    string(7) "Cardiff"
    [2]=>
    string(9) "Edinburgh"
    [0]=>
    string(6) "London"
}

Now, if you were to try to access that using a for loop and use $capitalcities[$i], you would find the data in the original order. This is because even though "London" is last in the array, it still uses the key 0 and will therefore be accessed first.

There are two ways to solve this problem. First, you can use a foreach loop to access the array in its natural order; this is the preferred method of accessing arrays anyway, and you should use it if you can. Alternatively, you can use the array_values() function to effectively reset the array keys.

By default, the sort functions sort so that 2 comes before 10. While this might be obvious, consider how a string sort would compare 2 and 10 - it would work character by character, which means it would compare 2 against 1 and therefore put 10 before 2. Sometimes this is the desired behaviour, and so you can pass a second parameter to the sort functions to specify how you want the values sorted, like this:

<?php
    $array["1"] = "someval1";
    $array["2"] = "someval2";
    $array["3"] = "someval3";
    $array["10"] = "someval4";
    $array["100"] = "someval5";
    $array["20"] = "someval6";
    $array["200"] = "someval7";
    $array["30"] = "someval8";
    $array["300"] = "someval9";
    var_dump($array);
    ksort($array, SORT_STRING);
    var_dump($array);
?>

If you want to force a strictly numeric sort, you can pass SORT_NUMERIC as the second parameter.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Grabbing keys and values >>

Previous chapter: Swapping keys and values

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.