| [ Team LiB ] |
|
21.2 Built-In Sorting FunctionsUsually, it will not be necessary to write your own sort functions. PHP offers several functions for sorting arrays. The most basic is sort. This function is described, along with the other sorting functions in Chapter 11. It's instructive to compare sort to rsort, asort, and ksort. The sort function puts all the elements in the array in order from lowest to highest. If the array contains any strings, then this means ordering them by the ASCII codes of each character. If the array contains only numbers, then they are ordered by their values. The indices—the values used to reference the elements—are discarded and replaced with integers starting with zero. This is an important effect, which Listing 21.1 demonstrates; Figure 21.1 shows the output. Notice that although I use some numbers and a string to index the array, after I sort it, all the elements are numbered zero through four. Keep this in mind if you ever need to clean up the indices of an array. Listing 21.1 Sorting with sort
<?php
/*
** Fill fruit array with random values
*/
$fruit[1] = "Apple";
$fruit[13] = "apple";
$fruit[64] = "Blueberry";
$fruit[3] = "pear";
$fruit["last"] = "Watermelon";
//sort the array
sort($fruit);
//dump array to show new order
print("<pre>");
print_r($fruit);
print("</pre>\n");
?>
Figure 21.1. Output of Listing 21.1.
Another point worth noting in Listing 21.1 is the order of the output: Apple, Blueberry, Watermelon, apple, pear. A dictionary might list apple just before or just after Apple, but the ASCII code for A is 65. The ASCII code for a is 97. Appendix B lists all the ASCII codes. Later in this chapter I'll explain how to code a case-insensitive sort. The rsort function works exactly like sort except that it orders elements in the reverse order. Try modifying the code in Listing 21.1 by changing sort to rsort. Two other two sort functions, asort and arsort, work in a slightly different way. They preserve the relationship between the index and the element. This is most useful when you have an associative array. If the array is indexed by numbers, you probably do not want to preserve their indices. On the other hand, what if you did? Listing 21.2 illustrates a possible scenario; output is shown in Figure 21.2. Listing 21.2 Using the asort function
<?php
// Fill and array in order of preference
$pasta = array(1=>"ravioli",
"spaghetti",
"vermicelli",
"lasagna",
"gnocchi",
"rigatoni");
// Sort the array, keeping indices
asort($pasta);
// Print array, now in alphabetical order
foreach($pasta as $rank=>$name)
{
print("$name was ranked number $rank<br>\n");
}
?>
Figure 21.2. Output of Listing 21.2.
Listing 21.2 gets each element in the order in which the elements exist in memory. They retain their original indices, which are the numbers starting with zero used when the elements were added to the array. If I had used arsort, the order would have been the exact opposite. Listing 21.3 is perhaps a more typical use of these functions. It is important to keep the elements in the array returned by getdate associated with their indices. Listing 21.3 sorts the array in reverse order by the elements. It may not be particularly useful but illustrates the use of this function. The output is shown in Figure 21.3. Listing 21.3 Using the arsort function
<?php
//get an array from getdate
$today = getdate();
//Sort the array, keeping indices
arsort($today);
//Print array, now in descending order
print("<pre>");
print_r($today);
print("</pre>\n");
?>
Figure 21.3. Output of Listing 21.3.
The last sorting function I want to discuss in this section is ksort. This function sorts an array on the values of the indices. I've modified the code in Listing 21.3 to use ksort instead of arsort. Notice that now all the elements are in the order of their indices, or keys. The ksort function is perhaps most useful in situations where you have an associative array and you don't have complete control over the contents. In Listing 21.4 the script gets an array generated by the getdate function. If you run it with the ksort line commented out, you will see that the order is arbitrary. It's simply the order chosen when the function was coded. I could have typed a couple of lines for each element based on the list of elements found in the description of the getdate function in Chapter 14. A more readable solution is to sort on the keys and to print each element in a loop. As you might have guessed, the krsort function sorts an array by its indices in reverse. Listing 21.4 Using the ksort function
<?php
// get an array from getdate
$today = getdate();
// Sort the array, keeping indices
ksort($today);
//Print array, now ordered by keys
print("<pre>");
print_r($today);
print("</pre>\n");
?>
|
| [ Team LiB ] |
|