PHP 8.4.0 RC3 available for testing

arsort

(PHP 4, PHP 5, PHP 7, PHP 8)

arsort連想キーと要素との関係を維持しつつ配列を降順にソートする

説明

arsort(array &$array, int $flags = SORT_REGULAR): true

各配列のキーと要素との関係を維持しつつ、array を降順にソートします。

この関数は、主に実際の要素の並び方が重要である連想配列をソートするために使われます。

注意:

比較結果が等しくなる二つの要素があった場合、それらの並び順は保持されます。PHP 8.0.0 より前のバージョンでは、ソートした配列におけるそれらの並び順は不定でした。

注意:

この関数をコールすると、配列の内部ポインタは最初の要素にリセットされます。

パラメータ

array

入力の配列。

flags

オプションの第二引数 flags によりソートの動作を修正可能です。 使える値は下記の通りです:

ソートタイプのフラグ:

  • SORT_REGULAR - 通常通りに項目を比較します。 詳細は 比較演算子 で説明されています。
  • SORT_NUMERIC - 数値として項目を比較します。
  • SORT_STRING - 文字列として項目を比較します。
  • SORT_LOCALE_STRING - 現在のロケールに基づいて、文字列として項目を比較します。 比較に使うロケールは、setlocale() 関数で変更できます。
  • SORT_NATURAL - 要素の比較を文字列として行い、 natsort() と同様の「自然順」で比較します。
  • SORT_FLAG_CASE - SORT_STRINGSORT_NATURAL と (ビットORで) 組み合わせて使い、 文字列のソートで大文字小文字を区別しないようにします。

戻り値

常に true を返します。

変更履歴

バージョン 説明
8.2.0 戻り値の型が、true になりました。これより前のバージョンでは、bool でした。

例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 はアルファベットの降順にソートされ、 各要素とキーとの関係は維持されます。

参考

add a note

User Contributed Notes 3 notes

up
13
morgan at anomalyinc dot com
24 years ago
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
up
11
stephenakins at gmail dot com
7 years ago
I have two servers; one running 5.6 and another that is running 7. Using this function on the two servers gets me different results when all of the values are the same.

<?php

$list
= json_decode('{"706":2,"703":2,"702":2,"696":2,"658":2}', true);

print_r($list);

arsort($list);
echo
"<br>";

print_r($list);

?>

PHP 5.6 results:
Array ( [706] => 2 [703] => 2 [702] => 2 [696] => 2 [658] => 2 )
Array ( [658] => 2 [696] => 2 [702] => 2 [703] => 2 [706] => 2 )

PHP 7 results:
Array ( [706] => 2 [703] => 2 [702] => 2 [696] => 2 [658] => 2 )
Array ( [706] => 2 [703] => 2 [702] => 2 [696] => 2 [658] => 2 )
up
-1
FatBat
13 years ago
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.
?>
To Top