PHP 8.3.4 Released!

rsort

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

rsort配列を降順にソートする

説明

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

array を、値で降順にソートします。

注意:

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

注意: この関数は、 array パラメータの要素に対して新しいキーを割り当てます。 その際、単純にキーを並べ替える代わりに、 すでに割り当てられている既存のキーを削除してしまいます。

注意:

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

パラメータ

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 rsort() の例

<?php
$fruits
= array("lemon", "orange", "banana", "apple");
rsort($fruits);
foreach (
$fruits as $key => $val) {
echo
"$key = $val\n";
}
?>

上の例の出力は以下となります。

0 = orange
1 = lemon
2 = banana
3 = apple

fruits はアルファベットの降順にソートされました。

参考

add a note

User Contributed Notes 6 notes

up
3
Alex M
18 years ago
A cleaner (I think) way to sort a list of files into reversed order based on their modification date.

<?php
$path
= $_SERVER[DOCUMENT_ROOT]."/files/";
$dh = @opendir($path);

while (
false !== ($file=readdir($dh)))
{
if (
substr($file,0,1)!=".")
$files[]=array(filemtime($path.$file),$file); #2-D array
}
closedir($dh);

if (
$files)
{
rsort($files); #sorts by filemtime

#done! Show the files sorted by modification date
foreach ($files as $file)
echo
"$file[0] $file[1]<br>\n"; #file[0]=Unix timestamp; file[1]=filename
}
?>
up
1
ray at non-aol dot com
19 years ago
Like sort(), rsort() assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys. This means that it will destroy associative keys.

$animals = array("dog"=>"large", "cat"=>"medium", "mouse"=>"small");
print_r($animals);
//Array ( [dog] => large [cat] => medium [mouse] => small )

rsort($animals);
print_r($animals);
//Array ( [0] => small [1] => medium [2] => large )

Use KSORT() or KRSORT() to preserve associative keys.
up
-5
slevy1 at pipeline dot com
22 years ago
I thought rsort was working successfully or on a multi-dimensional array of strings that had first been sorted with usort(). But, I noticed today that the array was only partially in descending order. I tried array_reverse on it and that seems to have solved things.
up
-8
pshirkey at boosthardware dot com
19 years ago
I needed a function that would sort a list of files into reversed order based on their modification date.

Here's what I came up with:

function display_content($dir,$ext){

$f = array();
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($folder = readdir($dh)) !== false) {
if (preg_match("/\s*$ext$/", $folder)) {
$fullpath = "$dir/$folder";
$mtime = filemtime ($fullpath);

$ff = array($mtime => $fullpath);
$f = array_merge($f, $ff);

}
}



rsort($f, SORT_NUMERIC);

while (list($key, $val) = each($f)) {
$fcontents = file($val, "r");
while (list($key, $val) = each($fcontents))
echo "$val\n";
}

}
}

closedir($dh);
}

Call it like so:

display_content("folder","extension");
up
-10
rnk-php at kleckner dot net
20 years ago
Apparently rsort does not put arrays with one value back to zero. If you have an array like: $tmp = array(9 => 'asdf') and then rsort it, $tmp[0] is empty and $tmp[9] stays as is.
up
-13
suniafkhami at gmail dot com
10 years ago
If you are sorting an array from a database result set, such as MySQL for example, another approach could be to have your database sort the result set by using ORDER BY DESC, which would be the equivalent of using rsort() on the resulting array in PHP.

[Edited by moderator for clarity: googleguy at php dot net]
To Top