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:
$colorList = array("apple"=>"red",
"grass"=>"green",
"sky"=>"blue",
"night"=>"black",
"wall"=>"white");
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:
foreach ($colorList as $key => $value) {
echo $key.' - '.$value.'<br/>';
}
then your output will look like this:
Output:
apple - red
grass - green
sky - blue
night - black
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:
And the output will be:
Output:
0 - black
1 - blue
2 - green
3 - red
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:
And in this case the output will be:
Output:
0 - white
1 - red
2 - green
3 - blue
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:
And the output will be:
Output:
night - black
sky - blue
grass - green
apple - red
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:
which results the following output:
Output:
apple - red
grass - green
night - black
sky - blue
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:
Post a Comment