PHP for the World Wide Web (Visual QuickStart Guide)

I l @ ve RuBoard

PHP supports a variety of ways to sort an array (when I say sort, I am referring to an alphabetical sort if it is a string, and a numerical sort if it is a number). When sorting an array, you must keep in mind that an array consists of several pairs of keys and values. Thus, an array can be sorted based upon the values or the keys. Also, you can sort the values and keep the corresponding keys matched up or sort the values and have them receive 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 like this:

function($Array); So, sort() and rsort() are simply: sort($Array); rsort($Array);

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

To sort by the keys, while still maintaining the correlation between the key and its value, you use ksort(). Conversely, krsort() will sort the keys in reverse.

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

As an example of sorting arrays, you'll create a list of students and the grades they received on a test, then sort this list first by grade then by name .

To sort an array:

  1. Open your text editor and create a new PHP document entitled.

  2. Begin with the standard HTML and PHP document code (Script 7.5):

    Script 7.5. There are a number of different functions available in PHP for sorting arrays, including arsort() and ksort() which are used here.

    <HTML><HEAD><TITLE>Sorting Arrays </TITLE><BODY><?php

  3. Create the array:

    $Grades = array( "Richard"=>"95", "Sherwood"=>"82", "Toni"=>"98", "Franz"=>"87", "Melissa"=>"75", "Roddy"=>"85" );

  4. Print a caption and then print each element of the array using a loop.

    print ("Originally, the array looks like this:<BR>"); for ($n = 0; $n < count($Grades); $n++) { $Line = each ($Grades); print ("$Line[key]'s grade is $Line[value].<BR>\n"); }

  5. Sort the array in reverse order by values to determine who had the highest grade.

    arsort($Grades);

    Because you are determining who has the highest grade, you need to use arsort() instead of asort(). The latter, which sorts the array by numeric order, would order them 75, 82, 85, etc. and not the desired 98, 95, 87, etc.

    You also must use arsort() and not just rsort() in order to maintain the key-value relationship (which rsort() would eradicate).

  6. Reset the array's pointer using the reset() function.

    reset($Grades);

    Just to make sure that the loop begins with the first element of the $Grade array, you use the reset() function which returns the pointer to the first element in the array. This has to be done because the previous loop moved the pointer to the end of the array.

  7. Print the array again (with a caption), using another loop.

    print ("<P>After sorting the array by value using arsort(), the array looks like this:<BR>"); for ($n = 0; $n < count($Grades); $n++) { $Line = each ($Grades); print ("$Line[key]'s grade is $Line[value].<BR>\n"); }

  8. Now sort the array by key to put the array in alphabetical order by student name and reset the array again.

    ksort($Grades); reset($Grades);

    The ksort() function will organize the array by key (in this case, alphabetically ) while maintaining the key-value correlation.

  9. Print a caption and the array one last time.

    print ("<P>After sorting the array by key using ksort(), the array looks like this:<BR>"); for ($n = 0; $n < count($Grades); $n++) { $Line = each ($Grades); print ("$Line[key]'s grade is $Line[value].<BR>\n"); }

  10. Close the script with the standard PHP and HTML tags:

  11. ?></BODY></HTML>

  12. Save your script as sort.php, upload it to the server, and test in your Web browser (Figure 7.7).

    Figure 7.7. You can sort an array in a number of ways with varied results. Pay close attention to whether or not you want to maintain your key-value association when choosing a sort function.

I l @ ve RuBoard

Категории