1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4. <head>
5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6. <title>My Little Gradebook</title>
7. </head>
8. <body>
9. <?php // Script 7.5 - sort.php
10. /* This script creates, sorts, and prints out an array. */
11.
12. // Address error management, if you want.
13.
14. // Create the array:
15. $grades = array(
16. 'Richard' => 95,
17. 'Sherwood' => 82,
18. 'Toni' => 98,
19. 'Franz' => 87,
20. 'Melissa' => 75,
21. 'Roddy' => 85
22. );
23.
24. // Print the original array:
25. print '<p>Originally the array looks like this: <br />';
26. foreach ($grades as $student => $grade) {
27. print "$student: $grade<br />\n";
28. }
29. print '</p>';
30.
31. // Sort by value in reverse order, then print again.
32. arsort ($grades);
33. print '<p>After sorting the array by value using arsort(), the array looks like this: <br />';
34. foreach ($grades as $student => $grade) {
35. print "$student: $grade<br />\n";
36. }
37. print '</p>';
38.
39. // Sort by key, then print again.
40. ksort ($grades);
41. print '<p>After sorting the array by key using ksort(), the array looks like this: <br />';
42. foreach ($grades as $student => $grade) {
43. print "$student: $grade<br />\n";
44. }
45. print '</p>';
46.
47. ?>
48. </body>
49. </html>