繁体   English   中英

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

[英]How to sort an array of numeric strings which also contain numbers. (natural ordering) in PHP

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

例如,我已经使用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);

自然排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM