©
This document uses PHP Chinese website manual Release
(PHP 4, PHP 5, PHP 7)
arsort — 对数组进行逆向排序并保持索引关系
&$array
[, int $sort_flags
= SORT_REGULAR
] )本函数对数组进行排序,数组的索引保持和单元的关联。
主要用于对那些单元顺序很重要的结合数组进行排序。
array
输入的数组。
sort_flags
可以用可选的参数 sort_flags
改变排序的行为,详情见 sort() 。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 arsort() 例子
<?php
$fruits = array( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
arsort ( $fruits );
foreach ( $fruits as $key => $val ) {
echo " $key = $val \n" ;
}
?>
以上例程会输出:
a = orange d = lemon b = banana c = apple
fruits 被按照字母顺序逆向排序,并且单元的索引关系不变。
[#1] sebas2day [2012-05-07 14:52:09]
If you are dealing with a multidimensional array you want to sort, then this might be helpfull:
<?php
function array_sort($arr){
if(empty($arr)) return $arr;
foreach($arr as $k => $a){
if(!is_array($a)){
arsort($arr); // could be any kind of sort
return $arr;
}else{
$arr[$k] = array_sort($a);
}
}
return $arr;
}
?>
[#2] FatBat [2011-10-13 13:40:07]
Needed to get the index of the max/highest value in an assoc array.
max() only returned the value, no index, so I did this instead.
<?php
reset($x); // optional.
arsort($x);
$key_of_max = key($x); // returns the index.
?>
[#3] jordancdarwin at googlemail dot com [2007-12-15 14:21:21]
A lot of people seem to trip up on this and ask me questions as to debugging. Bear in mind that this returns boolean, and does not return an array of affected items.
$array = array("One"=>1, "Three" => 3,"Two" =>2);
print_r(asort($array));
If successful, will return 1, and error if there is a string used. Useful to note so then people stop asking me :D
[#4] rodders_plonker at yahoo dot com [2000-08-21 18:43:33]
I was having trouble with the arsort() function on an older version of PHP which was returning an error along the lines of 'wrong perameter count for function arsort' when I tried to use a flag for numeric sorting (2/SORT_NUMERIC).
I figured, as I only wanted to sort integers, I could pad numbers from the left to a specific length with 0's (using the lpad function provided by improv@magma.ca in the notes at http://www.php.net/manual/ref.strings.php).
A string sort then correctly sorts numerically (i.e. {30,2,10,21} becomes {030,021,010,002} not {30,21,2,10}) when echoing the number an (int)$string_name hides the leading 0's.
Made my day :).
Rodders.
[#5] morgan at anomalyinc dot com [1999-11-24 19:30:31]
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