Skip to content
Advertisement

Sort Multiple Array to Numeric order (1,2,…,9,10,11) and Alphabetical order in PHP

How can I sort multidimensional array in numeric order first and then in alphabetical order in PHP. I have one array :

JavaScript

I want this array to sort in Numeric first and rest are in alphabetical order. So basically want array output like this :

JavaScript

I tried natsort and array_multisort but not able to get desired output. Any idea how to do this?

Advertisement

Answer

You can use usort on the pin_list element of the array (assuming you have decoded this JSON to an associative array), using strnatcmp to compare the name values:

JavaScript

Demo on 3v4l.org

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