| Implementing Arrays |
| Article Index |
|---|
| Implementing Arrays |
| Page 2 |
| Page 3 |
| Page 4 |
| Page 5 |
| Page 6 |
| Page 7 |
| Page 8 |
| Page 9 |
| Page 10 |
Although a number of sorting functions are available to the PHP developer, this particular sorting function has been chosen because of one critical detailit maintains the association of key/value pairs (see the discussion of array_filter() earlier in this chapter for more information on the importance of this). The syntax for the asort() function is as follows:
NOTEThe asort() function orders arrays from smallest to largest (or alphabetically from AZ). If the opposite behavior is desired, the arsort() function can be used in the same manner as asort(). |
$input represents the array to be sorted, and $sort_flag is an optional parameter (a constant) specifying the type of sort to perform. Note that the constants specified by the sort_flag parameter are not PHP variables. They are constant values predefined within PHP (much like what can be created using the define statement). The three possible values for the sort_flag parameter are shown in :
|
SORT_REGULAR |
Compare the items normally (default). |
|
SORT_NUMERIC |
Compare the items as numeric values. |
|
SORT_STRING |
Compare the items as string values. |
When executed, the asort() function will sort the $input array as determined by sort_flag (no return value).
Implementing the asort() function to sort and maintain your table structure becomes a relatively simple task because each column of your table is stored in a separate array. Use the asort() function to sort whichever column is desired, and then use the foreach() function to display the sorted data in an HTML table, as shown in , which sorts the table by the Weight column:








