超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<arrayasort>
Last updated: Fri, 22 Jun 2007

arsort

(PHP 3, PHP 4 , PHP 5)

arsort --  对数组进行逆向排序并保持索引关系

说明

void arsort ( array array [, int sort_flags])

本函数对数组进行排序,数组的索引保持和单元的关联。主要用于对那些单元顺序很重要的结合数组进行排序。

例子 1. arsort() 例子

<?php
$fruits
= array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
arsort ($fruits);
reset ($fruits);
while (list (
$key, $val) = each ($fruits)) {
   echo
"$key = $val\n";
}
?>

本例输出如下:

a = orange
d = lemon
b = banana
c = apple

fruits 被按照字母顺序逆向排序,并且单元的索引关系不变。

可以用可选的参数 sort_flags 改变排序的行为,详情见 sort()

参见 asort()rsort()ksort()sort()




add a note add a note User Contributed Notes
arsort
morgan at anomalyinc dot com
25-Nov-1999 11:30
If you need to sort a multi-demension array, for example, an array such as

$TeamInfo[$TeamID]["WinRecord"]
$TeamInfo[$TeamID]["LossRecord"]
$TeamInfo[$TeamID]["TieRecord"]
$TeamInfo[$TeamID]["GoalDiff"]
$TeamInfo[$TeamID]["TeamPoints"]

and you have say, 100 teams here, and want to sort by "TeamPoints":

first, create your multi-dimensional array. Now, create another, single dimension array populated with the scores from the first array, and with indexes of corresponding team_id... ie
$foo[25] = 14
$foo[47] = 42
or whatever.
Now, asort or arsort the second array.
Since the array is now sorted by score or wins/losses or whatever you put in it, the indices are all hoopajooped.
If you just walk through the array, grabbing the index of each entry, (look at the asort example. that for loop does just that) then the index you get will point right back to one of the values of the multi-dimensional array.
Not sure if that's clear, but mail me if it isn't...
-mo

<arrayasort>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22