PHP natcasesort

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP: natcasesort() function

PHP: Sort an array using a case insensitive natural order algorithm

The natcasesort() function is used to sort an array using a case insensitive "natural array" algorithm. The function implements a sort algorithm but maintains original keys/values.

natcasesort() is a case insensitive version of natsort().

Version

(PHP 4 and above)

Syntax

natcasesort(array_name)

Parameter

Name

Description

Required /
Optional

Type

array_name

The input array.

Required

Array

 

Return value

TRUE on success or FALSE on failure.

Value Type : Boolean.

Example :

  1. $php_files = array("code12.php", "Code22.txt",  

  2. "code2.php", "code3.php", "Code1.php");  

  3. sort($php_files);  

  4. echo "List of file using standard sorting: ";  

  5. print_r($php_files);  

  6. echo "
    ";  

  7. natcasesort($php_files);  

  8. echo "List of file using natural order: ";  

  9. print_r($php_files);  

  10. ?>  

Output :

List of file using standard sorting: Array ( [0] => Code1.php [1] => Code22.txt [2] => code12.php [3] => code2.php [4] => code3.php )
List of file using natural order: Array ( [0] => Code1.php [1] => Code22.txt [3] => code2.php [4] => code3.php [2] => code12.php )

Pictorial Presentation :

php array natcasesort() function