Sort an array using a natural order algorithm, keeping the original keys.
<?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 natsort()
function sorts an array using a “natural order” algorithm, which treats numbers in a human-friendly manner.
For example, in natural order sorting, 2 is considered less than 10, whereas in computer-based sorting, 10 would be considered less than 2. The function retains the original array k
natsort(array) |
Parameter |
Description |
array |
Required. Specifies the array to be sorted. |
Return Value: |
Returns TRUE on success or FALSE on failure. |
PHP Version: |
4+ |
PHP Changelog: |
PHP 5.2.1: Zero-padded numeric strings (e.g., ‘00006’) now ignore the leading zeros. |