如何对包含数字的字符串数组进行排序。
例如,我使用了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"
所需输出
"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"
有没有PHP函数可以执行此操作?
非常感谢
使用natsort
此函数实现了一种排序算法,该算法在维护键/值关联的同时,以人类的方式对字母数字字符串进行排序。这被描述为"自然排序"。
sort($array, SORT_NATURAL);
或
natsort($array);
自然排序。