Skip to content
Advertisement

PHP Sort Array By SubArray Value

I have the following array structure:

JavaScript

What is the best way to order the array in an incremental way, based on the optionNumber?

So the results look like:

JavaScript

Advertisement

Answer

Use usort.

JavaScript

In PHP ≥5.3, you should use an anonymous function instead:

JavaScript

Note that both code above assume $a['optionNumber'] is an integer. Use @St. John Johnson’s solution if they are strings.


In PHP ≥7.0, use the spaceship operator <=> instead of subtraction to prevent overflow/truncation problems.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement