PHP natsort() Function
Complete PHP Array Reference
Definition and Usage
The natsort() 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 returns TRUE on success, or FALSE on
failure.
Syntax
Parameter |
Description |
array |
Required. Specifies the array to sort |
Example
<?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 output of the code above will be:
Standard sorting: Array
(
[0] => temp1.txt
[1] => temp10.txt
[2] => temp15.txt
[3] => temp2.txt
[4] => temp22.txt
)
Natural order: Array
(
[0] => temp1.txt
[3] => temp2.txt
[1] => temp10.txt
[2] => temp15.txt
[4] => temp22.txt
)
|
Complete PHP Array Reference
Learn XML with <oXygen/> XML Editor - Free Trial!
|
|
oXygen helps you learn to define,
edit, validate and transform XML documents. Supported technologies include XML Schema,
DTD, Relax NG, XSLT, XPath, XQuery, CSS.
Understand in no time how XSLT and XQuery work by using the intuitive oXygen debugger!
Do you have any XML related questions? Get free answers from the oXygen
XML forum
and from the video
demonstrations.
Download a FREE 30-day trial today!
|
|