How to sort a multidimensional array based on key?

→ Are you a new visitor? Please visit the page guidance for new visitors ←

How to sort a multidimensional array based on key?

First of all, let’s give credit to the person who wrote this function.

The problem ?

Let’s say you have a multidimensional array and you want to sort based on a key that is not on the first level of the array, how would you do that ?

Given this array, you will want probably to sort based on “age” or “sex”, keys that are in the second level or arrays. You can obtain that with the following function:

Calling this function with the appropriate parameters, allow you to obtain the desired structure, so:

will output this:

You can sort on any other key. It’s your choice, I`m here just to provide this good function further to the people who need it 🙂

 

 

Request an article ←