natcasesort

Sorts an array by value using a case-insensitive natural order algorithm, preserving keys.

Signature
natcasesort(array &$array): true
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$arrayarrayThe array to sort. Passed by reference and modified directly; keys are preserved.

Return value

trueAlways returns true. The array is sorted in place through the reference.

Errors

Nothing. A non-array argument raises a TypeError from the parameter type.

Changelog

  • PHP 8.2The return type is true now; previously it was bool.
  • PHP 8.0Elements that compare as equal keep their original order; before 8.0 their relative order was undefined.

And zero-padded numbers are compared digit by digit rather than numerically, so a004 sorts before a04, which sorts before a4 — strnatcasecmp('a04', 'a4') returns -1, not 0. (Verified on PHP 8.5.9: natsort(['a4','a04','a004']) yields a004, a04, a4.)

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Order a directory listing of screenshots named IMG1.png, img2.png, IMG10.png the way a file browser would.
  • Sort user-entered tags or category names alphabetically without letting capitalisation split near-duplicates.
  • Present numbered chapter titles like 'Part 2' and 'part 10' in reading order.

Frequently asked questions

What is the difference between natsort and natcasesort?

They use the same natural-order algorithm; natcasesort ignores letter case while natsort treats uppercase letters as sorting before lowercase ones. Use natcasesort when the casing of your data is inconsistent.

Does natcasesort preserve array keys?

Yes, key/value associations are kept, so the sorted array may have out-of-order numeric keys. Call array_values() on the result if you need a clean 0-based list.

How do I sort in reverse natural order?

There is no built-in reverse variant; sort with natcasesort and then call array_reverse($arr, true), or use uasort($arr, fn($a, $b) => strnatcasecmp($b, $a)).

More examples

Compared with sort and natsort

sort gives File1,File10,file2,file20 — pure byte order, so every capital F beats every lowercase f. natsort fixes the numbers but still groups by case. natcasesort gives File1,file2,File10,file20, which is what a reader expects.

Example · php
<?php
$base = ['File10', 'file2', 'File1', 'file20'];

$a = $base; sort($a);
$b = $base; natsort($b);
$c = $base; natcasesort($c);

echo 'sort:        ', implode(',', $a), "\n";
echo 'natsort:     ', implode(',', $b), "\n";
echo 'natcasesort: ', implode(',', $c), "\n";

Reindexing for JSON output

The first line prints {"2":"beta1","0":"beta9","1":"Beta10"} — a JSON object, because the preserved keys are no longer 0,1,2 in order. array_values renumbers them and the second line prints a proper array.

Example · php
<?php
$names = ['beta9', 'Beta10', 'beta1'];
natcasesort($names);

echo json_encode($names), "\n";
echo json_encode(array_values($names)), "\n";

Discussion

  • Be the first to comment on this lesson.