如何对包含数字的数字字符串数组进行排序.(自然排序)在PHP中

Tar*_*nes -1 php arrays sorting glob

如何对包含数字的字符串数组进行排序.

例如,我使用了glob()函数来获取文件名列表.

默认情况下,数组按升序输出文件,但单独读取每个数字字符而不是整数.

默认输出

"C://path/to/file/file.tpl"
"C://path/to/file/file1.tpl"
"C://path/to/file/file11.tpl"
"C://path/to/file/file12.tpl"
....
....
"C://path/to/file/file2.tpl"
Run Code Online (Sandbox Code Playgroud)

要求的输出

"C://path/to/file/file.tpl"
"C://path/to/file/file1.tpl"
"C://path/to/file/file2.tpl"
...
...
"C://path/to/file/file11.tpl"
"C://path/to/file/file12.tpl"
Run Code Online (Sandbox Code Playgroud)

是否有PHP函数执行此操作?

非常感谢

Bar*_*tra 7

使用natsort

此函数实现了一种排序算法,该算法按照人类维护键/值关联的方式对字母数字字符串进行排序.这被描述为"自然排序".


Bit*_*ive 6

sort($array, SORT_NATURAL);

或者

natsort($array);

自然排序。