I have this website using arrays and then I have this function which sorts these arrays using asort
. It looks like this:
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
This algorithm sorts the array from 1-10 but I'll need it to sort descending, from 10-1. I have tried using rsort
with no luck, and I have tried array_reverse
too without luck. I don't know if I have used them wrong? Or.. Well at least I just need the algorithm to sort them descending. Any idea, advice or suggestions are appreciated. Thanks!
Try usort
with defined ordering function as the documentation says.
http://www.php.net/manual/en/function.usort.php
function sortSomething($a, $b){
if ($a < $b){
return -1;
}
else if ($a > $b){
return 1;
}
else{
return 0;
}
};
// Now sort the array using the comparison function
usort($array, 'sortSomething');
This sorts elements in a normal way - just switch the comparison operators and you'll get reverse sorting.