PHP natcasesort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, natcasesort() sorts an array of strings in natural order, ignoring uppercase and lowercase, and keeps the keys.

Natural order is how a person would sort: file2 comes before file10. A normal sort puts file10 before file2 because it compares character by character. natcasesort() fixes that, and it also treats "IMG" and "img" as the same.

Syntax

Call natcasesort() with the values you need. Start with this line.

PHP
natcasesort($files);
CallWhat it does
natcasesort($files)Sorts file names naturally and ignores the case.
natsort($files)The same natural sort, but uppercase letters come before lowercase.
array_values($files)Use this after natcasesort() if you want keys 0, 1, 2 again.

natcasesort() changes the array in place and returns true. The keys stay with their values, so the keys may look out of order after sorting. It is the case-insensitive version of natsort().

Library declarations
PHP
natcasesort(array &$array): true

Parameters

ParameterDescription
arrayRequired. The array to sort. It is passed by reference and changed in place.

Return Value

Return ValueDescription
trueAlways returns true. The sorted result is stored in the array you passed in.

PHP Examples

Example 1 — Sort File Names Naturally

The file names contain numbers and mixed capitals. print_r() shows that the keys are kept.

PHP
<?php
$files = ["img12.png", "img10.png", "IMG2.png", "img1.png"];

natcasesort($files);

print_r($files);

How It Works

  1. The array has four file names with numbers inside them.
  2. natcasesort() compares the numbers as numbers, so 1 comes before 2, then 10, then 12.
  3. The capital letters in "IMG2" do not matter because the case is ignored.
  4. Each name keeps its original key, which print_r() shows as 3, 2, 1, 0.

Example 2 — Compare with a Normal sort()

A normal sort compares character by character. natcasesort() understands whole numbers.

PHP
<?php
$a = ["img12.png", "img10.png", "img2.png", "img1.png"];
$b = $a;

sort($a);
natcasesort($b);

echo implode(", ", $a), PHP_EOL;
echo implode(", ", array_values($b)), PHP_EOL;

How It Works

  1. $b is a copy of $a, so both arrays start with the same four names.
  2. sort() compares text, so "img10" and "img12" come before "img2".
  3. natcasesort() reads 2, 10 and 12 as numbers, so img2 comes before img10.
  4. array_values() is used on $b so the output ignores the preserved keys.

Example 3 — Case-Insensitive Versus Case-Sensitive

natsort() treats capitals as smaller than lowercase letters. natcasesort() ignores the case.

PHP
<?php
$words = ["Banana", "apple", "cherry"];
$one = $words;
$two = $words;

natsort($one);
natcasesort($two);

echo implode(", ", array_values($one)), PHP_EOL;
echo implode(", ", array_values($two)), PHP_EOL;

How It Works

  1. Two copies of the same array are made so they can be sorted in different ways.
  2. natsort() is case sensitive, so "Banana" (capital B) comes before "apple".
  3. natcasesort() ignores the case, so the order is apple, Banana, cherry.
  4. array_values() removes the preserved keys so the output is a simple list.

Frequently Asked Questions

It sorts the values of an array in natural order and ignores uppercase and lowercase letters. The keys are preserved.
It is the way humans sort: numbers inside strings are compared as whole numbers. That gives file1, file2, file10 instead of file1, file10, file2.
natsort() is case sensitive and puts capital letters first. natcasesort() ignores the case.
Yes. Each value keeps its original key. Use array_values() afterwards if you want the keys to be 0, 1, 2 again.
No. It sorts the array you pass in and returns true.

Next: natsort()

natsort() sorts an array in natural order, the way a person would, and keeps the keys.

natsort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful