PHP natsort () function
Examples
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);
?>
$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);
?>
Running instance »
Definition and Usage
natsort () function uses a "natural order" algorithm to sort the array. Keys retain their original keys.
Natural sorting algorithm, the number is smaller than the number 10. In computer sorting algorithms, 10 is less than 2, because the "10" the first number is less than 2.
grammar
natsort( array )
参数 | 描述 |
---|---|
array | 必需。规定要进行排序的数组。 |
technical details
return value: | If successful it returns TRUE, on failure returns FALSE. |
---|---|
PHP version: | 4+ |
Update log: | Since PHP 5.2.10 onwards, when filled with zeros numeric string (eg '00006') will be ignored 0. |
Complete PHP Array Reference