arsort

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

arsort Sortiert ein Array in absteigender Reihenfolge und behält die Indexzuordnung bei

Beschreibung

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

Sortiert array in absteigender Reihenfolge so, dass der Zusammenhang zwischen den Schlüsseln und den Werten, die ihnen zugeordnet sind, erhalten bleibt.

Dies wird hauptsächlich zur Sortierung assoziativer Arrays verwendet, bei denen die aktuelle Reihenfolge der Elemente von Bedeutung ist.

Hinweis:

Wenn zwei Mitglieder als identisch verglichen werden, behalten sie ihre ursprüngliche Reihenfolge bei. Vor PHP 8.0.0 war die relative Sortierung im sortierten Array nicht definiert.

Hinweis:

Setzt den internen Zeiger des Arrays auf das erste Element zurück.

Parameter-Liste

array

Das Eingabe-Array.

flags

Der optionale zweite Parameter flags kann mit folgenden Werten genutzt werden, um das Sortierverhalten zu ändern:

Flags für den Sortiertyp:

Rückgabewerte

Gibt immer true zurück.

Changelog

Version Beschreibung
8.2.0 Der Rückgabewert ist nun true vorher war es bool.

Beispiele

Beispiel #1 arsort()-Beispiel

<?php
$fruits
= array("d" => "Zitrone", "a" => "Orange", "b" => "Banane", "c" => "Apfel");
arsort($fruits);
foreach (
$fruits as $key => $val) {
echo
"$key = $val\n";
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

d = Zitrone
a = Orange
b = Banane
c = Apfel

Die Früchte wurden in umgekehrter alphabetischer Reihenfolge sortiert und die Zuordnung zwischen Indizes und Elementen blieb erhalten.

Siehe auch

add a note add a note

User Contributed Notes 6 notes

up
11
stephenakins at gmail dot com
6 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
14
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
-2
FatBat
12 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.
?>
up
-6
rodders_plonker at yahoo dot com
23 years ago
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.
up
-9
jordancdarwin at googlemail dot com
16 years ago
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
up
-15
sebas2day
11 years ago
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;
}
?>
To Top