PHP Tutorial - PHP natsort() Function






Definition

The natsort() function sorts an array by using a "natural order" algorithm.

Syntax

PHP natsort() Function has the following syntax.

natsort(array)

Parameter

ParameterIs RequiredDescription
arrayRequired.Specifies the array to sort

Note

This function is case-sensitive.

Return

This function returns TRUE on success, or FALSE on failure.





Example 1

Sort an array:


<?php/*from  w w  w.  j a va 2 s  .  co  m*/
$temp_files = array("a15","A10","A1","A22","A2");

sort($temp_files);
echo "Standard sorting: ";
print_r($temp_files);
echo "\n";

natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
?>

The code above generates the following result.