Sort alphanumeric strings in PHP
by sabitha[ Edit ] 2012-06-05 16:34:02
Sort alphanumeric strings in PHP
This function sorts an array using a "natural order" algorithm. This is used when handling alphanumeric strings. We can sort the alphanumeric strings in ascending order using this function.
$myarr1 = $myarr2 = array("rose12.png", "rose10.png", "rose2.png", "rose1.png");
asort($myarr1); //standard sorting
print_r($myarr1);
Standard sorting using asort will outputs as
Array
(
[3] => rose1.png
[1] => rose10.png
[0] => rose12.png
[2] => rose2.png
)
But using natsort the output differs
natsort($myarr2);
print_r($myarr2);
output is:
Array
(
[3] => rose1.png
[2] => rose2.png
[1] => rose10.png
[0] => rose12.png
)