PHP natcasesort() Function
Example
Sort an array by using natsort() and natcasesort():
<?php
$temp_files = array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
echo "<br />";
natcasesort($temp_files);
echo "Natural order case insensitve: ";
print_r($temp_files);
?>
The output of the code above will be:
Natural order:
Array
(
[0] => Temp10.txt
[1] => Temp22.txt
[2] => temp1.txt
[4] => temp2.txt
[3] => temp15.txt
)
Natural order case insensitve:
Array
(
[2] => temp1.txt
[4] => temp2.txt
[0] => Temp10.txt
[3] => temp15.txt
[1] => Temp22.txt
)
Definition and Usage
The natcasesort() function sorts an array by using a "natural order" algorithm. The values keep their original keys.
In a natural algorithm, the number 2 is less than the number 10. In computer sorting, 10 is less than 2, because the first number in "10" is less than 2.
This function is case-insensitive.
Syntax
natcasesort(array)
Parameter Values
Parameter | Description |
---|---|
array | Required. Specifies the array to sort |
Technical Details
Return Value: | TRUE on success. FALSE on failure |
---|---|
PHP Version: | 4+ |
❮ PHP Array Reference