Sort array items in PHP so that it is not case sensitive to letters

梦想与她 提交于 2019-12-18 05:44:24

问题


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

  1. Apple
  2. Green
  3. Zebra
  4. grass

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. Apple
  2. Green
  3. grass
  4. Zebra

回答1:


Call usort() as usort($topics, 'strnatcasecmp').

strcasecmp would do the job, too, but strnatcasecmp will also sort properly when you have numbers in your string.




回答2:


There is natcasesort .

natcasesort($topics);


来源:https://stackoverflow.com/questions/7763936/sort-array-items-in-php-so-that-it-is-not-case-sensitive-to-letters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!