Site Cloner PHP Script
Bargain Hunter PHP Script
Job Hunter PHP Script
Site Login and Access Control PHP Script

PHP Sort Array

Sorting arrays in PHP can be done with various functions. A multitude of sort functions can be found at PHP.net. One very common function to sort an array is with the simple sort() function.

Use PHP sort() function in its own line to sort an array. For example, date and datetime entries could be sorted by ascending. The sort function is placed on a line on its own after the original array is made. As you can see below, the array is sorted in alphabetical order.

$myarray = array("one", "two", "three"); 
sort($myarray); 
print_r($myarray);   //yields Array (     [0] => one     [1] => three     [2] => two )

Depending upon your needs you may need to use a different sort function; since some will sort by key and some will sort by value. For example, take a look at the associative array below.The example below is sorted by key using ksort().

 $myarray = array("one" => "first", "two" => "second", "three" => "third");  
ksort($myarray); 
print_r($myarray);     //yields Array (     [one] => first     [three] => third     [two] => second )