PHP support for arrays is fantastic, particularly for those of us with a history using ASP. Sorting a single-column array is straightforward (there is a sort() function). But what about sorting a multi-column array? Well, there is a function (array_multisort()) that does it, but understanding how to use it is a challenge. This article hopefully provides a simpler explanation.
If you are using a multi-column array to store data from a database, it is much simpler and faster to get the data directly from the database in the order required (use the ORDER BY clause in your SELECT statement). This function is best used for multi-column arrays that are generated locally, or only in part from a database.
How to
To sort a multi-column array:
- determine which columns you want to sort by
- for each column you want to sort by, copy its contents into a separate, single-column array
- build your array_multisort() command:
- add the new single-column array as a parameter
- optionally follow the array with parameters for sort order and type
- repeat above two steps for each column to be sorted on
- complete command with name of multi-column array to be sorted
sort type and order
Sort type can be:
- SORT_REGULAR – compare/sort normally (default)
- SORT_NUMERIC – compare/sort as numbers
- SORT_STRING – compare/sort as strings
Sort regular uses standard ASCII order (“ABa” is 3 characters in regular order). The other two options operate more logically (eg “aAB” is the same characters now in string order).
Sort order can be:
- SORT_ASC – ascending order (default)
- SORT_DESC – descending order
For example…
For example. here’s a 3-column array we want to sort:
<?php $data[0]['name']= 'Google'; $data[0]['url'] = 'http://www.google.com'; $data[0]['rank'] = 45; $data[1]['name']= 'Yahoo'; $data[1]['url'] = 'http://www.yahoo.com'; $data[1]['rank'] = 17; $data[2]['name']= 'MSN'; $data[2]['url'] = 'http://www.msn.com'; $data[2]['rank'] = 6; ?>
Assume we want to sort this array by rank (ascending order). The first step is to store the rank column as a separate, single-column array. This is easy to do using the foreach command:
<?php
foreach ($data as $val)
{
$sortArrayRank[] = $val['rank'];
}
?>
To sort by this column, use array_multisort and pass our newly created rank array as the first parameter:
<?php array_multisort($sortArrayRank, $data); ?>
To change the sort to descending order, add the required order parameter after the 1-column array:
<?php array_multisort($sortArrayRank, SORT_DESC, $data); ?>
To sort by more than one column (eg to sort by rank, then name), simply repeat the above steps for the subsequent column(s) and add the paramaters to the list.
<?php
foreach ($data as $val)
{
$sortArrayName[] = $val['name'];
}
array_multisort($sortArrayRank, SORT_DESC, $sortArrayName, $data);
?>
more information
The official explanation of array_multisort can be found in the PHP manual.
October 6th, 2010 at 3:18 pm
That is really good article.
it was very helpful for me.
thanks guy.
January 7th, 2011 at 2:04 am
Из вас мудрейший тот, кто знает, что ничего-то по правде не стоит его мудрость.
July 17th, 2011 at 6:30 am
I have been dealing with a sorting issue for a publisher catalog on and off for years. I finally found this page and you have explained everything so well that I was able to get the site just as I want it. Thank you for taking the time to post such a concise (and accurate) tutorial.
September 28th, 2011 at 5:57 am
Three more entries and you will almost be as experienced as I am.