在PHP中对数组项进行排序,以便对字母不区分大小写 [英] Sort array items in PHP so that it is not case sensitive to letters

查看:199
本文介绍了在PHP中对数组项进行排序,以便对字母不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用sort($topics)时,我得到了类似的东西:

When I use sort($topics) I get something along the lines of:

  1. 苹果
  2. 绿色
  3. 斑马

在此示例中,"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?

  1. 苹果
  2. 绿色
  3. 斑马

推荐答案

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屋!

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