Backend Development 2 min read

Using natcasesort() for Case‑Insensitive Natural Order Sorting in PHP

This article explains the PHP natcasesort() function, which performs a case‑insensitive natural order sort on arrays while preserving key/value associations, details its parameters and return value, and provides a complete example with code and expected output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using natcasesort() for Case‑Insensitive Natural Order Sorting in PHP

The natcasesort() function in PHP is the case‑insensitive version of natsort() , implementing a natural order sorting algorithm that keeps the original keys associated with their values.

Parameters

array &$array – the input array to be sorted.

Return value

Returns TRUE on success, or FALSE on failure.

Example

The following code demonstrates standard sorting with sort() and natural order case‑insensitive sorting with natcasesort() :

<?php
$array1 = $array2 = array(
    'IMG0.png',
    'img12.png',
    'img10.png',
    'img2.png',
    'IMG3.png'
);

sort($array1);
echo "Standard sorting\n";
print_r($array1);

natcasesort($array2);
echo "\nNatural order sorting (case‑insensitive)\n";
print_r($array2);
?>

Output

Standard sorting
Array
(
    [0] => IMG0.png
    [1] => IMG3.png
    [2] => img1.png
    [3] => img10.png
    [4] => img12.png
)

Natural order sorting (case‑insensitive)
Array
(
    [0] => IMG0.png
    [1] => IMG3.png
    [2] => img1.png
    [3] => img10.png
    [4] => img12.png
)
backendPHParraynatural sortingcase-insensitivenatcasesort
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.