Sort array elements in PHP so that it is not case sensitive

When I use sort($topics) , I get something line by line:

  • Apple
  • Green
  • Zebra
  • grass

In this example, grass begins with lowercase g, but ends after Zebra, which has a capital letter.

How to make it sort it where it ignores, does the word begin with capital or not?

  • Apple
  • Green
  • grass
  • Zebra
+7
source share
2 answers

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

strcasecmp will do the job too, but strnatcasecmp will also sort correctly when you have numbers on your line.

+16
source

There is natcasesort .

 natcasesort($topics); 
+12
source

All Articles