Sort the array:
<?php$temp_files = array("temp15.txt","temp10.txt","temp1.txt","temp22.txt","temp2.txt");sort($temp_files);echo "Standard sorting: ";print_r($temp_files);echo "<br>";natsort($temp_files);echo "Natural order: ";print_r($temp_files);?>The natsort() function sorts an array using the "natural sorting" algorithm. Key values retain their original key names.
In the natural sorting algorithm, the number 2 is less than the number 10. In computer sorting algorithms, 10 is less than 2 because the first number in "10" is less than 2.
natsort( array )
parameter | describe |
---|---|
array | Required. Specifies the array to be sorted. |
Return value: | Returns TRUE if successful and FALSE if failed. |
---|---|
PHP version: | 4+ |
Update log: | As of PHP 5.2.10, when padding a numeric string with zeros (eg '00006'), the zeros are ignored. |