downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

asort> <array
Last updated: Fri, 24 Jul 2009

view this page in

arsort

(PHP 4, PHP 5)

arsort배열을 내림차순 정렬하고 인덱스의 상관관계를 유지

설명

bool arsort ( array &$array [, int $sort_flags ] )

이 함수는 배열 인덱스가 그 배열의 원소와 상관관계를 유지할수 있도록 배열을 정렬한다.

이 함수는 주로 실제 원소의 정렬이 중요한 연관 배열을 정렬할 때 이용한다.

인수

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는 알파벳 내림 차순으로 정렬되었고, 각 원소와 연관된 인덱스가 유지되었다.

  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • rsort() - 역순으로 배열 정렬
  • ksort() - 키에 의한 배열 정렬
  • sort() - 배열 정렬



asort> <array
Last updated: Fri, 24 Jul 2009
 
add a note add a note User Contributed Notes
arsort
jordancdarwin at googlemail dot com
15-Dec-2007 10: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
Scott Woods
02-Feb-2005 04:21
Note about "morgan at anomalyinc dot com"'s comment:

As of PHP4, you can just use array_multisort() to sort parallel or multi-dimensional arrays.
rodders_plonker at yahoo dot com
22-Aug-2000 01:43
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.
morgan at anomalyinc dot com
25-Nov-1999 03: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

asort> <array
Last updated: Fri, 24 Jul 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites