This question already has an answer here:
- How can I sort arrays and data in PHP? 9 answers
I'm trying to sort my PHP hashtable based on a specifc key. The datastructure looks like this:
print_r($mydata);
Array(
[0] => Array
(
[type] => suite
[name] => A-Name
)
[1] => Array
(
[type] => suite
[name] => C-Name
)
[2] => Array
(
[type] => suite
[name] => B-Name
)
)
I've tried ksort, sort, usort but nothing seems to work. I'm trying to sort based on the name key two-levels down.
This was my attempt using usort:
function cmp($a, $b) {
return $b['name'] - $a['name'];
}
usort($mydata, "cmp");
Is there an easy way to do this or do I need to write a custom sort function?
try this recursive mode, that I used in Magento REST API:
Thinking,more useful and practical http://php.net/manual/en/function.sort.php
How to use
Output=>
Try this usort function:
If you using it in a class, the second parameter changes to an array like this:
array_multisort() - Sort multiple or multi-dimensional arrays