Example 1 — Sort Numbered File Names
The keys are kept, so print_r() shows the sorted order along with the original positions.
<?php
$files = ["img12.png", "img10.png", "img2.png", "img1.png"];
natsort($files);
print_r($files); How It Works
- The array has four names that contain numbers.
- natsort() compares the numbers inside the names as whole numbers.
- The order becomes 1, 2, 10, 12, which is what a person expects.
- The keys 3, 2, 1, 0 show that every value kept its old key.

