PHP array_multisort – sorting a multi-dimensional array as you like

I successfully avoided this subject for quite a while – but now I finally wanted to know: how do I sort a multi-dimensional array in PHP to my personal preferences? The quick answer is: with the nady little PHP function called array_multisort.

Why / When do I need to sort a multi-dimensional array?

Good question. Simple database queries are often just one-dimensional. For example you want to query a list of clients from a CRM System in alphabetical order – and a simple mySQL query can of course do that for you. In another context you might want to re-use the query – but this time order the list of clients by date of birth. You could just run another query – or instead you could re-arrange or actually sort the array you already have differently.

I actually have to deal with multi-dimensional arrays quite often – but in most cases it’s enough to sort them while I do the mySQL query. But in the long run it’s of course not the most elegant way to have extra mySQL queries just because you want your array to be sorted differently. While it may first look convenient, this approach sure is not very practical in the long run.

PHP array_multisort to the rescue!

As the name suggests the little PHP function array_multisort can elegantly do the job. No ‘expensive’ extra queries needed – your scrips will run faster and will be easier to maintain. So whenever you can re-use a db-query, go ahead and re-use it. You might want to thank yourself when looking at your own scripts moths or years from when you originally coded them.

One example for PHP array_multisort, just to get started: a database query gives me the following output:

Array
(
    [0] => Array
        (
            [id] => 369
            [name] => Peter
            [year] => 1969 
        )

    [1] => Array
        (
            [id] => 368
            [name] => Paul
            [year] => 1975
        )

    [2] => Array
        (
            [id] => 367
            [name] => Mary
            [year] => 1977
        )

)

As you can see the array contains some three records that consist of three fields: IDs, names and years. The array is ordered by the year. Now I would like to use the very same array, but order it by name. I just have to ‘pull the array apart’ and rearrange it according to my preferences, using array_multisort. The following little function will just do that:

$sortArray = array();

        foreach($entryArray as $entry){
            foreach($entry as $key=>$value){
                if(!isset($sortArray[$key])){
                    $sortArray[$key] = array();
                }
                $sortArray[$key][] = $value;
            }
        } 
    $orderby = "name";
    array_multisort($sortArray[$orderby],SORT_ASC,$entryArray); 
    
return $entryArray;

In this case $entryArray is my original array, containing the results of my original mySQL query. It may look a bit cryptic at first glance, but once implemented it actually works like a charm. After the reordering the array looks like this:

Array
(
    [0] => Array
        (
            [id] => 367
            [name] => Mary
            [year] => 1977 
        )

    [1] => Array
        (
            [id] => 368
            [name] => Paul
            [year] => 1975
        )

    [2] => Array
        (
            [id] => 369
            [name] => Peter
            [year] => 1969
        )

)

Very handy indeed.

See also:

  • PHP-Manual > Function Reference > … > Array Functions > array_multisort

Leave a Reply

Your email address will not be published. Required fields are marked *