PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

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:

What Are Arrays?
How To Create Arrays
How To View Arrays
How To Modify Arrays
Array arsort() Function
Array asort() Function
Array rsort() Function
Array ksort() Function
Array krsort() Function
Array Echo
Array list() Function
Manual Array Iteration
Array count() & sizeof() Function




Home | Privacy Policy | Terms Of Use | Contact Us