Richard Hedges
Richard Hedges

Reputation: 1188

Display an array 'in order'?

I've got an array called $rank_array:
Array ( [Tribus Bella] => 179 ) Array ( [TestClan] => 767 )

When I run this code:

foreach ($rank_array as $clan => $rank) {
    echo $clan.' = '.$rank.'<br />';
}

I get the following:

Tribus Bella = 179
TestClan = 767

I'd like to display it in the reverse order (so the it's ordered by the $rank variable), but when I use something like asort it doesn't change the order at all.
Can anyone help explain why? And help me fix it?

edit
None of the functions seem to be working (arsort, asort, etc) so I'm wondering if it's the way I'm inserting the data into the array.

I'm inserting it with this code

$rank_array = array($q['name'] => $clan_total_points);

Is that wrong?

Upvotes: 1

Views: 116

Answers (2)

DaveRandom
DaveRandom

Reputation: 88697

The default sort flag for asort() is SORT_REGULAR, which will sort in ascending order - which the order in which they already are. You need to sort into descending order, which you would do like this:

asort($rank_array, SORT_DESC);

Now when you loop $rank_array, it will be in the order in which you want it. Wrong!

As @Nameless has correctly pointed out, the correct answer to this question is that you need to use arsort() in order to achieve what you want.

Upvotes: 5

Nameless
Nameless

Reputation: 2366

For more-to-less value sorting use arsort function.

Upvotes: 1

Related Questions