|
| SORTING ARRAYS WITH THE sort() FUNCTION
PHP provides many ways to sort an array if you need to. PHP stores an array in the order in which you create them. However, you can later change this order, for example, to alphabetical value order or by key order.
You can use the sort() function to sort the values to alphabetical order if the keys are numbers.
The general format for the sort() function is:
sort($array);
This sorts the array by value, sorted with numbers first ascending, then uppercase letters alphabetically, then lowercase letters alphabetically. The keys are re-assigned with appropriate numbers.
Example:
$username[0]="Elmafudd";
$username[1]="8hotchick";
$username[2]="aopper24";
$username[3]="11_daper";
sort($username);
The array is now.
$username[0] = 11_daper
$username[1] = 8hotchick
$username[2] = Elmafudd
$username[3] = aopper24
If you use arrays with text as keys with the above function, the keys will be replaced by numbers. To sort arrays with text as keys and retain the text keys, use the asort() function instead.
See also:
|
|