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: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises