Chapter 7 Exercises

Exercise 6: Sorting Arrays:

With PHP there are a variety of ways to sort an array (sort refers to an alphabetical sort if the values being sorted are strings, or a numerical sort if the values being sorted are numbers). When you are sorting an array, you must keep in mind that an array consists of pairs of keys and values. Thus, an array can be sorted based on the keys or the values. This is further complicated by the fact that you can sort the values and keep the corresponding keys aligned, or you can sort the values and have them be assigned new keys.

To sort the values without regard to the keys, you use sort(). To sort these values (again, without regard to the keys) in reverse order, you use rsort(). The syntax for every sorting function is:

The syntax for every sorting function in PHP.

So, sort() and rsort() are used as follows:

The PHP sort(), rsort() function.

To sort the values while maintaining the correlation between each value and its key, you use asort(). To sort the values in reverse while maintaining the key correlation, you use arsort().

To sort by the keys while maintaining the correlation between the key and its value, you use ksort(). Conversely, krsort() sorts the keys in reverse. Table 7-2 lists all these functions.

Table 7-2: These functions sort arrays in different ways.

List of sorting functions in PHP.

Finally, shuffle() randomly reorganizes the order of an array.


In this exercise you will create a list of students and the grades they receive on a test. You will then sort this list, first by grade and then by name.

Note: A number of Helpful Tips are located at the end of this web page for reference as you complete this exercise. There is also an Advanced Topic for additional learning.

To sort an array:

  1. You will create a PHP script (Script 7-5):
    Script 7-5: PHP provides a number of different functions for sorting arrays, including arsort() and ksort() (usedhere).
    The PHP script that includes arsort() and ksort() as displayed in the editor view.
  2. To begin, create a new HTML document (Script 7-5) with the title My Little Gradebook:
    A new HTML document with the title My Little Gradebook as displayed in the editor view.
  3. Begin the PHP section, and address error handling, if necessary:
    The PHP code snippet that begins the PHP section, and addresses error handling as displayed in the editor view.
  4. Create the array:
    The PHP $grades array as displayed in the editor view.
    The $grades array consists of six students’ names along with their corresponding grades. Because the grades are number values, they do not need to be quoted when assigning them.
  5. Print a caption, and then print each element of the array using a foreach loop:
    The PHP code snippet that prints a caption, and then prints each element of the array using a foreach loop as displayed in the editor view.
    The caption tells you what point in the script you are at. At first, it prints the array in the original order. For this you will use a foreach loop, where each index (the student’s name) is assigned to $student, and each value (the student’s grade) is assigned to $grade. The final print() statement closes the HTML paragraph.
  6. Sort the array in reverse order by value to determine who has the highest grade:
    The PHP arsort() function example that sorts the array in reverse order by value to determine who has the highest grade as displayed in the editor view.
    To determine which student has the highest grade, you need to use arsort() instead of asort(). The latter, which sorts the array in numeric order, would order the grades 75, 82, 85, and so on, rather than the desired 98, 95, 87. You must also use arsort() and not rsort() in order to maintain the key-value relationship (rsort() would eliminate the student’s name associated with each grade).
  7. Print the array again (with a caption), using another loop:
    The PHP code snippet that prints the array again(with a caption), using another loop as displayed in the editor view.
  8. Sort the array by key to put the array in alphabetical order by student name:
    The PHP ksort() function example that sorts the array by key to put the array in alphabetical order by student name as displayed in the editor view.
    The ksort() function organizes the array by key (in this case, alphabetically) while maintaining the key-value correlation.
  9. Print a caption and the array one last time:
    The PHP code snippet that prints a caption and the array one last time as displayed in the editor view.
  10. Close the script with the standard PHP and HTML tags:
    The closing tags that close the script with the standard PHP and HTML tags as displayed in the editor view.
  11. Save the script as sort.php to your XAMPP folder (c:\XAMPP\htdocs\202\chapter7).
  12. Using Cyberduck, upload the file to the production server, and test in your browser (Figure 7-8), making sure you are looking at the production server version of the file.
    Figure 7-8: You can sort an array in a number of ways with varied results. Pay close attention to whether you want to maintain your key-value association when choosing a sort function.
    The HTML script that sorts an array in a number of ways with varied results as displayed in the browser view.
Important: If something doesn’t look right in your document, and you need to make changes to the script, follow the editing procedure using Synapse outlined in Chapter 1.

 

Quick tips icon.

  • The $grades array could have been created using the grades as the keys and the names of the students as values. It works either way.
  • The natsort() and natcasesort() functions sort a string (while maintaining key-value associations) using natural order. The most obvious example of natural order sorting is that it places name2 before name12, whereas sort() orders them name12 and then name2.
  • The usort(), uasort(), and ursort() functions let you sort an array using a user-defined comparison function. These functions are most often used with multidimensional arrays.

 

Additional/Advanced Reading Icon.

The extract() and list() array functions in PHP.


Return to Chapter 7, Assignments Page