PHP natcasesort function tutorial shows how to sort arrays using natural order case-insensitive algorithm. Learn with examples.
last modified March 13, 2025
The PHP natcasesort function sorts an array using a case-insensitive “natural order” algorithm. It maintains index association and is useful for sorting strings containing numbers.
natcasesort implements a natural order sorting algorithm while ignoring case differences. It’s similar to how humans would sort strings.
Syntax: natcasesort(array &$array): bool. The function sorts the array in place and returns true on success or false on failure.
Natural order sorting means numbers in strings are compared numerically rather than alphabetically. For example, “item2” comes before “item10”.
This demonstrates simple case-insensitive natural sorting of an array.
basic_natcasesort.php
<?php
$files = [“file1.txt”, “File10.txt”, “file2.txt”, “FILE20.txt”]; natcasesort($files);
print_r($files);
Output will be: Array ( [0] => file1.txt [2] => file2.txt [1] => File10.txt [3] => FILE20.txt ). The function ignores case and sorts numbers correctly.
Shows how natcasesort handles strings with varying case patterns.
mixed_case.php
<?php
$items = [“Apple”, “banana”, “apricot”, “Banana”, “apple”]; natcasesort($items);
print_r($items);
Case is ignored, placing all similar words together.
Demonstrates the natural order sorting of numbers within strings.
number_sorting.php
<?php
$versions = [“version-2”, “Version-10”, “version-1”, “VERSION-20”]; natcasesort($versions);
print_r($versions);
Numbers are sorted numerically despite case differences.
Shows that natcasesort preserves the original key-value relationships.
key_association.php
<?php
$data = [ “id3” => “Value C”, “ID1” => “Value A”, “id10” => “Value D”, “Id2” => “Value B” ]; natcasesort($data);
print_r($data);
Keys maintain their association with values after sorting.
Illustrates differences between natcasesort and regular sort functions.
comparison.php
<?php
$items = [“img1.png”, “Img10.png”, “img2.png”, “IMG12.png”];
$regular = $items; sort($regular);
$natural = $items; natsort($natural);
$naturalCase = $items; natcasesort($naturalCase);
echo “Regular sort:\n”; print_r($regular);
echo “\nNatural sort:\n”; print_r($natural);
echo “\nNatural case-insensitive sort:\n”; print_r($naturalCase);
Regular sort orders alphabetically (1,10,12,2). Natural sorts respect numbers but are case-sensitive. Natcasesort combines both natural order and case insensitivity.
Use when needed: Only for case-insensitive natural sorting.
Performance: Slower than simple sorts due to complex comparison.
Key preservation: Use when maintaining associations is important.
Mixed data: Works best with strings containing numbers.
This tutorial covered the PHP natcasesort function with practical examples showing its usage for natural case-insensitive array sorting.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.