Numerical arrays can be sorted rather easily by using sort(). However, a problem exists if the array contains both numerical and string values (for instance, "2" > "10" but 2 < 10). Therefore, the sorting can be tweaked so that a special data type is used for comparing elements when sorting:
Sorting an Array (sort.php)<pre> <?php $a = array('4', 31, '222', 1345); sort($a, SORT_NUMERIC); print_r($a); sort($a, SORT_STRING); print_r($a); ?> </pre> Here is the output of the preceding listing: Array ( [0] => 4 [1] => 31 [2] => 222 [3] => 1345 ) Array ( [0] => 1345 [1] => 222 [2] => 31 [3] => 4 )
|