English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP natsort() Function Usage and Example

PHP Array Function Manual

The natsort() function sorts an array using the 'Natural Sorting' algorithm

Syntax

natsort ( $array );

Definition and Usage

This function implements a sorting algorithm that sorts alphanumeric strings in a way similar to humans, while keeping the keys/Value Association. This is called 'Natural Sorting'.

Parameter

Serial NumberParameters and Description
1

array(Required)

It specifies an array

Return Value

This function returns TRUE on success and FALSE on failure.

Online Example

<?php
   $input1 = array('click.txt', 'img12.txt', 'img10.txt', 'img2.txt', 'img1.txt', 'IMG3.txt');
   $input2 = $input1;  
   natsort($input2);
   echo " \n Natural Sorting \n";
   print_r($input2);
?>
Test and See‹/›

Output Result:

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

   PHP Array Function Manual