Sorting Arrays

PHP provides tons of functions with which you can manipulate the arrays. Some of the handiest are the sorting functions. Take a look at Table 6.1 to see the list of sorting functions.

Table 6.1. Sorting Functions
Function Name Description
sort() Sorts arrays in alphabetical order.
asort() Sorts arrays in alphabetical order without changing indexes.
rsort() Sorts arrays in reverse order.
arsort() Sorts arrays in reverse order without changing indexes.
ksort() Sorts arrays by key values.
krsort() Sorts arrays by key values in reverse order.
usort() A customizable sort function.
uasort() A customizable sort function that keeps string indices.
uksort() A customizable sort function that sorts by key.
*A complete list of PHP functions is available in Appendix B.

Now take a closer look at each of these sorting functions. The sort() function sorts the elements in the array by numeric and then alphabetical order. Numbers come first, then punctuation marks, and finally letters. While the function is sorting, it reassigns the indexes to reflect the new order.

<?php
$items = array(“Sword”, “Medpac”, “Advanced Medpac”,
        “Armor”, “Blaster”, “Shotgun”);

// Print the unsorted items
for($index = 0; $index < count($items); $index++)
{
    echo(“Index = “ . $index . ”, “ . $items[$index] . ”<BR>”);
}

// Print the sorted items
sort($items);
for($index = 0; $index < count($items); $index++)
{
    echo(“Index = “ . $index . ”, “ . $items[$index] . ”<BR>”);
}
?>

The results of this example should look like Figure 6.6.

Figure 6.6. The sort() function in action.


Take a look at what happens when you have an array with specified indexes when you use the sort function.

<?php
$items = array(“Item1” => “Sword”, “Item2” =>  “Medpac”, “Item3” =>  “Advanced Medpac”,
    “Item4” =>  “Armor”, “Item5” => “Blaster”, “Item6” => “Shotgun”);

// Print the unsorted items
while(list($index, $value) = each($items))
{
    echo(“Index = “ . $index . ”, “ . $value . ”<BR>”);
}
echo(“<br>”);
// Print the sorted items
sort($items);
while(list($index, $value) = each($items))
{
    echo(“Index = “ . $index . ”, “ . $value . ”<BR>”);
}
?>

If you use the sort function with specified indexes, like in the example above, it will lose its indexes. Take a look at Figure 6.7 to see the results.

Figure 6.7. The sort() function with specified indexes.


To avoid losing your specified string indices you need to use the asort() function. This will sort the array and its elements without taking away your string indices. If you wanted to sort this array in reverse you would use the rsort() function. However, the rsort() function will also lose specified string indices, so to sort an array in reverse with string indices you need to use the arsort() function. You can also sort by your string indices by using ksort().

<?php
$items = array(“Item6” => “Sword”, “Item5” =>  “Medpac”, “Item3” =>  “Advanced Medpac”,
“Item4” =>  “Armor”, “Item2” => “Blaster”, “Item1” => “Shotgun”);
 
// Print the unsorted items
while(list($index, $value) = each($items))
{
    echo(“Index = “ . $index . ”, “ . $value . ”<BR>”);
}
echo(“<br>”);
// Print the sorted items
ksort($items);
while(list($index, $value) = each($items))
{
    echo(“Index = “ . $index . ”, “ . $value . ”<BR>”);
}
?>

The results of this example look like Figure 6.8.

Figure 6.8. The ksort() function in action.


NOTE

You can sort in reverse order by key by using the krsort() function.

The last of the sorting functions for arrays has to be the coolest. With usort() you can organize your arrays however you want. The usort() function takes two arguments: the first is the array you would like to sort, and the second is a name of a function that contains your own sorting logic.

usort(someArray, someFunction);

The following example will sort the array $items by the length of each string:

<?php
function SortByLength($element1, $element2)
{
$lengthOfElement1 = strlen($element1);
$lengthOfElement2 = strlen($element2);
if($lengthOfElement1 == $lengthOfElement2)
    return 0;
return ($lengthOfElement1 < $lengthOfElement2) ? -1 : 1;
}
 
$items = array(“Item6” => “Sword”, “Item5” =>  “Medpac”, “Item3” =>  “Advanced Medpac”,
“Item4“ =>  “Armor”, “Item2” => “Blaster”, “Item1” => “Shotgun”);
 
// Print the unsorted items
while(list($index, $value) = each($items))
{
    echo(“Index = “ . $index . ”, “ . $value . ”<BR>”);
}
echo(“<br>”);
// Print the sorted items
usort($items, ‘SortByLength’);
while(list($index, $value) = each($items))
{
    echo(“Index = “ . $index . ”, “ . $value . ”<BR>”);
}
?>

The results of the above example are displayed in Figure 6.9.

Figure 6.9. The usort() function in action.


NOTE

Make sure you pass the function name to usort() as a string; otherwise you will get a warning display in the middle of your page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset