PHP natcasesort() Function
The PHP natcasesort() function is used to sort an array according to the value and maintaining key to value association. This function implements a sort algorithm which uses "natural ordering" and sorts alphanumerically. This function is a case insensitive version of natsort() function.
Syntax
natcasesort(array)
Parameters
array |
Required. Specify the input array. |
Return Value
Returns true on success and false in failure.
Exceptions
NA.
Example:
In the example below, natcasesort() function is used to sort an array alphanumerically.
<?php $Arr = array("Red"=>"COLOR1", "Green"=>"Color2", "Blue"=>"color3", "White"=>"Color20"); asort($Arr); echo "Standard sorting\n"; print_r($Arr); natcasesort($Arr); echo "\nNatural order sorting (case-insensitive)\n"; print_r($Arr); ?>
The output of the above code will be:
Standard sorting Array ( [Red] => COLOR1 [Green] => Color2 [White] => Color20 [Blue] => color3 ) Natural order sorting (case-insensitive) Array ( [Red] => COLOR1 [Green] => Color2 [Blue] => color3 [White] => Color20 )
Example:
Consider one more example where this function is used to sort an indexed array alphanumerically.
<?php $Arr = array("color1", "Color2", "color3", "Color20", "COLOR15", "color25"); asort($Arr); echo "Standard sorting\n"; print_r($Arr); natcasesort($Arr); echo "\nNatural order sorting (case-insensitive)\n"; print_r($Arr); ?>
The output of the above code will be:
Standard sorting Array ( [4] => COLOR15 [1] => Color2 [3] => Color20 [0] => color1 [5] => color25 [2] => color3 ) Natural order sorting (case-insensitive) Array ( [0] => color1 [1] => Color2 [2] => color3 [4] => COLOR15 [3] => Color20 [5] => color25 )
❮ PHP Array Reference