Example 1 — Sort File Names Naturally
The file names contain numbers and mixed capitals. print_r() shows that the keys are kept.
<?php
$files = ["img12.png", "img10.png", "IMG2.png", "img1.png"];
natcasesort($files);
print_r($files); How It Works
- The array has four file names with numbers inside them.
- natcasesort() compares the numbers as numbers, so 1 comes before 2, then 10, then 12.
- The capital letters in "IMG2" do not matter because the case is ignored.
- Each name keeps its original key, which print_r() shows as 3, 2, 1, 0.

