PHP 8 reference · Array Sorting Functions

PHP natcasesort() Function

Sort an array in natural order while ignoring case, keeping keys. "Item2" comes before "item10".

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

natcasesort(array &$array): true

Parameters

ParameterDescription
array The array to sort (changed in place).

Return value

Always true.

Examples

Natural, case-insensitiveTry it yourself
<?php
$names = ["Item10", "item2", "ITEM1"];
natcasesort($names);
echo implode(", ", $names), "\n";

Output

ITEM1, item2, Item10
Compared with natsort()Try it yourself
<?php
$list = ["b2", "A10", "a2"];
natsort($list);
echo implode(", ", $list), "\n";
natcasesort($list);
echo implode(", ", $list), "\n";

Output

A10, a2, b2
a2, A10, b2

Version history

VersionChange
PHP 4.0Added
PHP 8.0Sorting is stable: elements that compare equal keep their original order.
PHP 8.2The return type is true (it was bool).

Was this page helpful?