Natural sorting of multi-dimensional in PHP

What is natural sorting

Natural sort order is an ordering of strings in alphabetical order, except that multi-digit numbers are ordered as a single character. Natural sort order has been promoted as being more human-friendly (“natural”) than the machine-oriented pure alphabetical order.[1]

For example, in alphabetical sorting “z11” would be sorted before “z2” because “1” is sorted as smaller than “2”, while in natural sorting “z2” is sorted before “z11” because “2” is sorted as smaller than “11”.

Source: Wikipedia

Natural sorting in PHP

In php there is a function called natsort which does exactly that. It does not however work on multi-dimensional arrays.
For example:

Will yield something similar:

How to naturally sort multi-dimensional array in PHP

In order to properly do a natural sort on multi-dimensional array based on keys and maintaining relations you can use the following function:

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*