PHP - 整理与数字文件名的数组? [英] PHP - sorting an array of filenames with numbers?

查看:104
本文介绍了PHP - 整理与数字文件名的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何排序包含以数字开头的文件名数组?因为,与11,12,13等被认为比2下开始字符串,它scewing我的结果是这样的:

Can anyone tell me how to sort an array containing filenames that start with numbers? Because strings that start with 11, 12, 13 etc are considered lower than 2, it's scewing my results like this:


[0] "1. File one.pdf"
[1] "11. File eleven.pdf"
[2] "12. File twelve.pdf"
[3] "2. File two.pdf"
[4] "3. File three.pdf"

有什么我可以做这些排序正确?

Is there anything I can do to sort these properly?

推荐答案

您可以使用 natsort 。或 natcasesort ,这是不区分大小写。如果有更多的统计数字(即变音符号),你应该确保你使用正确的区域

You can use natsort. or natcasesort, which is case insensitive. If there is more than numbers (ie. diacritics), you should assure that you use proper locale.

如果这是不够的,即得。你想一些文字也进行排序(一,二,三化),您可以使用 usort ,它允许您使用定义回调作为比较的功能。

If it is not enough, ie. you want also sort number literals ("one", "two", "three"), you can use usort, which permits to use your custom callback as comparison function.

这篇关于PHP - 整理与数字文件名的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆