按顺序显示数组?

问题描述:

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?

我有一个名为 $ rank_array strong>的数组:
数组([Tribus Bella] =&gt; 179)数组([TestClan] =&gt; 767) code>

当我运行此代码时: p> foreach($ rank_array as $ clan =&gt; $ rank){ echo $ clan。' ='。$ rank。'&lt; br /&gt;'; } code> pre>

我得到以下内容:
p> \ n

  Tribus Bella = 179 
TestClan = 767 
  code>  pre> 
 
 

我想以相反的顺序显示它(所以它是由它排序的 $ rank变量),但是当我使用 asort strong>这样的东西时,它根本不会改变顺序。
任何人都可以帮忙解释原因吗? 并帮我修复它? p>

编辑 strong>
其中一个函数似乎没有工作(arsort,asort等)所以我想知道是否 这是我将数据插入数组的方式。

我用这段代码插入它
p>

  $ rank_array = array(  $ q ['name'] =&gt; $ clan_total_points); 
  code>  pre> 
 
 

这是错的吗? p> div>

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

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.