I have some difficulty sorting my array. It looks like this :
[0] => Array
(
[firstname] => Jnic
[lastname] => Fortin
[points] => Array
(
[id] => 20453
[f] => 31
[r] => 7
[total] => 82
)
)
[1] => Array
(
[firstname] => Kris
[lastname] => Anders
[points] => Array
(
[id] => 20309
[f] => 0
[r] => 1
[total] => 56
)
)
[2] => Array
(
[firstname] => Em
[lastname] => Zajo
[points] => Array
(
[id] => 20339
[f] => 8
[r] => 3
[total] => 254
)
)
I would like to sort it by “total” DESC. How could I do it? If everything sort ok the array would be order [2][0][1] (254,82,56)
You probably can use the
usortfunction for that : it sorts an array, using a callback function to compare the elements of that array :If your function is defined to compare per
$element['points']['total'], it should do the trick.Edit : And here is the example, using
uasort, which is the same asusort, but will keep the array keys, like pointed out by ryanday :First, let’s declare the array :
And then, the comparison function :
And, finally, we use it :
And get the array, sorted by
totaldesc :ryanday > Thanks for your answer !