在PHP中对数组项进行排序,以便对字母不区分大小写 [英] Sort array items in PHP so that it is not case sensitive to letters
本文介绍了在PHP中对数组项进行排序,以便对字母不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用sort($topics)
时,我得到了类似的东西:
When I use sort($topics)
I get something along the lines of:
- 苹果
- 绿色
- 斑马
- 草
在此示例中,"grass"以小写g开头,但在带有大写字母的"Zebra"之后结束.
In this example, "grass" starts with a lower case g but ends up after "Zebra" which has a capital letter.
我如何使它排序以便忽略单词是否以大写字母开头的地方?
How do I make it so that it sorts it where it ignores whether the word starts with capitals or not?
- 苹果
- 绿色
- 草
- 斑马
推荐答案
将 usort()
称为usort($topics, 'strnatcasecmp')
strcasecmp
也可以完成这项工作,但是当您有数字时, strnatcasecmp
也会正确排序在您的字符串中.
strcasecmp
would do the job, too, but strnatcasecmp
will also sort properly when you have numbers in your string.
这篇关于在PHP中对数组项进行排序,以便对字母不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文