In all the countless hours I’ve spent with php, I’ve maybe used three or four of these sorting functions. I really had no idea that there is a total of eleven functions used for sorting arrays. Anyway, I’m betting that it may be useful to have these memorized before I take the Zend PHP Certification Exam so here is a brief overview of each one.
A lot of the sorting functions have an optional flag that may be used to modify the sorting behavior.
The available flags are:
SORT_REGULAR – compare items normally (don’t change types)
SORT_NUMERIC – compare items numerically
SORT_STRING – compare items as strings
SORT_LOCALE_STRING – compare items as strings, based on the current locale.
All of these functions sort arrays. I don’re really think it’s necessary to type out an example for each one but I will go into greater detail about some. Each function will return a boolean which is true on success and false on failure.
-
-
bool
sort ( array &
$array [, int
$sort_flags = SORT_REGULAR
] )
-
This sort function will arrange the elements from lowest to highest.
Array indices do NOT maintain their correlation with the array elements they are associated with.
-
-
bool
rsort ( array &
$array [, int
$sort_flags = SORT_REGULAR
] )
-
This one will arrange the elements from highest to lowest (Reverse Sort).
This sorting function will sort the array alphanumerically. This is more “natural” for most people.
Here is a comparison between the regular sort and this natural sort from the
php.net site:
-
-
<?php
-
$array1 =
$array2 =
array("img12.png",
"img10.png",
"img2.png",
"img1.png");
-
-
-
echo "Standard sorting\n";
-
-
-
-
echo "\nNatural order sorting\n";
-
-
?>
-
Which will output:
-
Standard sorting
-
-
(
-
[3] => img1.png
-
[1] => img10.png
-
[0] => img12.png
-
[2] => img2.png
-
)
-
-
Natural order sorting
-
-
(
-
[3] => img1.png
-
[2] => img2.png
-
[1] => img10.png
-
[0] => img12.png
-
)
-
A case insensitive version of natsort.
-
-
bool
ksort ( array &
$array [, int
$sort_flags = SORT_REGULAR
] )
-
Sorts an array by the key. It also maintains the key to data correlations.
-
-
bool
krsort ( array &
$array [, int
$sort_flags = SORT_REGULAR
] )
-
Sorts by key, but in reverse.
-
-
bool
asort ( array &
$array [, int
$sort_flags = SORT_REGULAR
] )
-
This is very similar to the regular sort outlined above except the array indices maintain
their correlation with the array elements they are associated with.
-
-
bool
arsort ( array &
$array [, int
$sort_flags = SORT_REGULAR
] )
-
Similar to the asort but reversed.
-
-
bool
usort ( array &
$array , callback
$cmp_function )
-
This function allows for a custom comparsion function to create different sorting methods. This function should accept only two parameters and return an integer less than, equal to,
or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. Here’s an example:
-
-
<?php
-
function cmp($a, $b)
-
{
-
if ($a == $b) {
-
return 0;
-
}
-
return ($a < $b) ? -1 : 1;
-
}
-
-
$a =
array(3,
2,
5,
6,
1);
-
-
-
-
foreach ($a as $key => $value) {
-
-
}
-
?>
-
-
This is similar to usort except the array indices maintain their
correlation with the array elements they are associated with.
This last sort function works similar to the previous two where you can define a custom comparison function. The difference is that this one works on the keys and it also maintains the correlation with the elements.
There is also a nice comparison of all these functions at php.net