PHP Array natcasesort() Function
Last Updated : 17 Mar 2025
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 and values. This function was introduced in PHP 4.0.
Syntax
Parameter
| Parameter | Description | Is compulsory |
|---|---|---|
| array | The input array. | compulsory |
Returns
The natcasesort( ) function returns true on success or false on failure.
Example 1
Output:
Standard sorting
Array
(
[0] => Rahul
[1] => Sachin
[2] =>sehwag
[3] =>virat
)
Natural order case insensitve: Array
(
[2] => Rahul
[0] => Sachin
[1] =>sehwag
[3] =>virat
)
Example 2
Output:
Standard sorting
Array
(
[0] => Code1.php
[1] => Code22.txt
[2] => code12.php
[3] => code2.php
[4] => code3.php
)
Natural order case insensitve: Array
(
[4] => Code1.php
[2] => code2.php
[3] => code3.php
[0] => code12.php
[1] => Code22.txt
)
Example 3
Output:
Standard sorting
Array
(
[0] => IMG0.png
[1] => IMG3.png
[2] => img1.png
[3] => img10.png
[4] => img12.png
[5] => img2.png
)
Natural order case insensitve: Array
(
[0] => IMG0.png
[4] => img1.png
[3] => img2.png
[5] => IMG3.png
[2] => img10.png
[1] => img12.png
)
Example 4
Output:
Standard sorting
Array
(
[0] => Java
[1] => Python
[2] => Swift
[3] =>php
)
Natural order case insensitve: Array
(
[0] => Java
[1] =>php
[3] => Python
[2] => Swift
)
Next TopicPhp-array-natsort-function