PHP - Function natsort()


Advertisements


Syntax

natsort ( $array );

Definition and Usage

This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering". An example of the difference between this algorithm and the regular computer string sorting algorithms (used in sort()) can be seen below −

Parameters

Sr.No Parameter & Description
1

array(Required)

It specifies an array

Return Value

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

Example

Try out following example −

Live Demo
<?php
   $input1 = array('click.txt', 'img12.txt', 'img10.txt', 'img2.txt', 'img1.txt', 'IMG3.txt');
   $input2 = $input1;  
   natsort($input2);
   echo " \n Natural order sorting \n";
   print_r($input2);
?> 

This will produce the following result −

Natural order sorting
Array ( [0] => click.txt [5] => IMG3.txt [4] => img1.txt [3] => img2.txt [2] => img10.txt [1] => img12.txt )

php_function_reference.htm

Advertisements