When sorting an array, for example by using sort() there are three constants you can use to determine how the sorting will work:
- SORT_REGULAR (default) when you have mixed types of variables in the array it won't cast them. The results can be ... hmm .. interesting. It will sort first all string types and them all numeric types
- SORT_NUMERIC - all elements will be casted to their numeric values
- SORT_STRING - all array elements will be sorted as strings.
Here's an example of the three options:
< ?php echo '
'; $a = array(3, 1, '01', '011', '0001', 'a', 'a0', 'b0', '0b0', '1a1', '0a0', 'xyz', '99a', 991, '992'); sort($a, SORT_REGULAR); // default print_r($a); sort($a, SORT_NUMERIC); print_r($a); sort($a, SORT_STRING); print_r($a); ?> The result will be:Array ( [0] => 01 [1] => 0001 [2] => 011 [3] => 0a0 [4] => 0b0 [5] => 1a1 [6] => 992 [7] => 99a [8] => a [9] => a0 [10] => b0 [11] => xyz [12] => 1 [13] => 3 [14] => 991 ) Array ( [0] => a0 [1] => a [2] => 0b0 [3] => 0a0 [4] => xyz [5] => b0 [6] => 01 [7] => 1a1 [8] => 0001 [9] => 1 [10] => 3 [11] => 011 [12] => 99a [13] => 991 [14] => 992 ) Array ( [0] => 0001 [1] => 01 [2] => 011 [3] => 0a0 [4] => 0b0 [5] => 1 [6] => 1a1 [7] => 3 [8] => 991 [9] => 992 [10] => 99a [11] => a [12] => a0 [13] => b0 [14] => xyz )