开发者

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

开发者 https://www.devze.com 2023-04-12 18:50 出处:网络
When I use sort($topics) I get something along the lines of: Apple Green Zebra grass In this example, \"grass\" starts with a lower case g but ends up after \"Zebra\" which has a capital letter.

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. 开发者_JAVA技巧
  4. grass
  5. Zebra


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.


There is natcasesort .

natcasesort($topics);
0

精彩评论

暂无评论...
验证码 换一张
取 消