natsort for the filenames that sorted in the wrong order

A directory listing sorted with sort() puts report-10.csv before report-2.csv, because string comparison reads character by character and 1 is less than 2. Nobody reports this as a bug; they report that the import processed the files out of order.

$files = array('report-1.csv', 'report-10.csv', 'report-2.csv', 'report-20.csv');

sort($files);
// report-1.csv, report-10.csv, report-2.csv, report-20.csv

natsort($files);
// report-1.csv, report-2.csv, report-10.csv, report-20.csv — keys preserved

sort($files, SORT_NATURAL | SORT_FLAG_CASE);
// the same order, keys renumbered from zero

usort($rows, function ($a, $b) {
    return strnatcasecmp($a['filename'], $b['filename']);
});

natsort() sorts in place and keeps the original keys, which is the difference that bites: the array is in the right order for a foreach and $files[0] is still whatever it was before, so anything indexing numerically needs array_values() afterwards. SORT_NATURAL passed to sort() gives the same comparison with renumbered keys, and strnatcmp() is the comparator when the strings are buried in rows rather than being the values themselves. None of these understand versions — 1.9 still sorts after 1.10 — and version_compare() is the function for that.