Array Manipulation With PHP4 - Sorting Things Out (
Page 7 of 8 )
If you need to, you
can rearrange the elements within an array with PHP's numerous sorting
functions. The simplest of these is the array_reverse() function, which merely
reverses the order of elements within an array.
<?
// create array
$trio = array("huey", "dewey", "louie");
// returns ("louie", "dewey", "huey")
array_reverse($trio);
?>
The shuffle() function randomly reshuffles the elements
within an array,
<?
// create array
$trio = array("huey", "dewey", "louie");
// shuffles elements of array to return ("dewey", "louie", "huey")
shuffle($trio);
?>
while the array_unique() function helps you strip out
duplicate values from an array.
<?
// create array
$clones = array("Tom", "Tom", "Harry", "Tom", "Harry", "Harry", "Harry",
"Tom");
// returns ("Tom", "Harry")
array_unique($clones);
?>
The sort() function can be used to sort an array
alphabetically or numerically,
<?
// create array
$animals = array("antelope", "zebra", "skunk", "baboon", "viper");
// returns ("antelope", "baboon", "skunk", "viper", "zebra")
sort($animals);
?>
while the rsort() function does the same thing (just the
other way around).
<?
// create array
$animals = array("antelope", "zebra", "skunk", "baboon", "viper");
// returns ("zebra", "viper", "skunk", "baboon", "antelope")
rsort($animals);
?>
The ksort() function sorts a hash by key (you can reverse the
sort order with the krsort() function),
<?
// create array
$numbers = array("2" => "duet", "13" => "baker's dozen", "-15" =>
"temperature", "3" => "stooges");
// returns ("-15" => "temperature", "2" => "duet", "3" => "stooges", "13"
=> "baker's dozen")
ksort($numbers);
// returns ("13" => "baker's dozen", "3" => "stooges", "2" => "duet", "-15"
=> "temperature")
krsort($numbers);
?>
The usort() function lets you apply your own sort function to
the elements of an array. The function that you define must be capable of
comparing two values, and must return a positive, negative or zero value
depending on whether the first value being compared is greater than, less than
or equal to the second value.
An example might help to make this clearer.
The following code snippet defines a custom sort function, which arranges
elements according to their length.
<?
// compare two values on length
function check_length($str1, $str2)
{
if (strlen($str1) > strlen($str2))
{
return 1;
}
elseif (strlen($str1) == strlen($str2))
{
return 0;
}
else
{
return -1;
}
}
// create array
$animals = array("antelope", "zebra", "skunk", "baboon", "viper", "yak");
// returns ("yak", "skunk", "viper", "zebra", "baboon", "antelope")
usort($animals, "check_length");
?>
In a similar manner, you can apply a user-defined comparison
function to the keys of a hash with the uksort() function - I'll leave this to
you to experiment with.
The natsort() function makes it possible to sort
array elements the way a human - rather than a computer - would. Consider the
following example:
<?
// create array
$mixed = array(1, "zebra", "skunk", "baboon", 13, "viper", -99, "yak");
// returns ("-99", "baboon", "skunk", "viper", "yak", "zebra", "1", "13")
sort($mixed);
// returns ("-99", "1", "13", "baboon", "skunk", "viper", "yak", "zebra")
natsort($mixed);
?>