w3resource

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:

<?php
$php_files = array("code12.php", "Code22.txt",
"code2.php", "code3.php", "Code1.php");
sort($php_files);
echo "List of file using standard sorting: ";
print_r($php_files);
echo "<br />";
natcasesort($php_files);
echo "List of file using natural order: ";
print_r($php_files);
?>

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 )

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: list
Next: natsort



PHP: Tips of the Day

PHP: Extract numbers from a string

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);