Saturday, August 16, 2008

PHP sort array

From time to time you want to sort arrays. You maybe want to do it alphabetically or numerically, ascending or descending order. To do this PHP provide some usefull functions. In the next section I will show you how to use them. First of all we need a small example array to work with:

Code:

  1. $colorList = array("apple"=>"red",

  2. "grass"=>"green",

  3. "sky"=>"blue",

  4. "night"=>"black",

  5. "wall"=>"white");

PHP F1



Now if you display the array you will see the elements in the same order as they were defined. (If you don't know how to print an array then you can visit our PHP print array tutorial.) So if you use the following code to display the unsorted array elements:



Code:

  1. foreach ($colorList as $key => $value) {

  2. echo $key.' - '.$value.'<br/>';

  3. }

PHP F1



then your output will look like this:



Output:

  1. apple - red

  2. grass - green

  3. sky - blue

  4. night - black

  5. wall - white



 



To sort an array in PHP you can use the built in sort and rsort functions. The difference between them is that rsort sorts the array in reverse order (descending) and sort makes it in normal order (ascending).

So let's see how it works. In this step we will sort the array with sort function before we display its content. The code looks like this:



Code:

  1. sort($colorList);

  2. foreach ($colorList as $key => $value) {

  3. echo $key.' - '.$value.'<br/>';

  4. }

PHP F1



And the output will be:



Output:

  1. 0 - black

  2. 1 - blue

  3. 2 - green

  4. 3 - red

  5. 4 - white



As you may noticed this function assigns new keys for the elements in array. Sort function will remove any existing keys you may have assigned, rather than just reordering the keys. I will show you a sollution for this problem later on.

If you want a reverse ordering the only thing you have to do is to use rsort intead of sort:



Code:

  1. rsort($colorList);

  2. foreach ($colorList as $key => $value) {

  3. echo $key.' - '.$value.'<br/>';

  4. }

PHP F1



And in this case the output will be:



Output:

  1. 0 - white

  2. 1 - red

  3. 2 - green

  4. 3 - blue

  5. 4 - black



 



As we see it before the sort and rsort functions are not usable if we want to maintain our array keys as well. Fortunately PHP has built in functions for this problem as well. The sollution is called asort and arsort. As before asort results an ascending and resort results a descending order. However they maintain the array keys as well. So let's see how these functions sort our associative arrays. We repeat our test with the new functions:



Code:

  1. asort($colorList);

  2. foreach ($colorList as $key => $value) {

  3. echo $key.' - '.$value.'<br/>';

  4. }

PHP F1



And the output will be:



Output:

  1. night - black

  2. sky - blue

  3. grass - green

  4. apple - red

  5. wall - white



Now it looks much better. To get a reverse order just use arsort instead of asort.



 



As you may suppose PHP has some function to sort an associative array by keys. And you are right. These functions are the ksort and krsort. As before the krsort sorts the array in a reverse order. The usage is the same as before so your code will be:



Code:

  1. ksort($colorList);

  2. foreach ($colorList as $key => $value) {

  3. echo $key.' - '.$value.'<br/>';

  4. }

PHP F1



which results the following output:



Output:

  1. apple - red

  2. grass - green

  3. night - black

  4. sky - blue

  5. wall - white



The usage of krsort is straightforward.

Beside this PHP has some more advanced sorting functions:


array_multisort — Sort multiple or multi-dimensional arrays
natcasesort — Sort an array using a case insensitive "natural order" algorithm
natsort — Sort an array using a "natural order" algorithm
uasort — Sort an array with a user-defined comparison function and maintain index association
uksort — Sort an array by keys using a user-defined comparison function
usort — Sort an array by values using a user-defined comparison function

No comments: