ConFoo Montreal 2017 Calling for Papers


(PHP 4, PHP 5, PHP 7)

arsortTrie un tableau en ordre inverse et conserve l'association des index


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

arsort() trie le tableau array de telle manière que la corrélation entre les index et les valeurs soit conservée.

L'usage principal est lors de tri de tableaux associatifs où l'ordre des éléments est important.


If two members compare as equal, their relative order in the sorted array is undefined.

Liste de paramètres


Le tableau d'entrée.


Vous pouvez modifier le comportement de cette fonction en utilisant le paramètre optionnel sort_flags. Pour plus de détails, voyez la fonction sort().

Valeurs de retour

Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.


Exemple #1 Exemple avec arsort()

= array("d" => "lemon""a" => "orange""b" => "banana""c" => "apple");
foreach (
$fruits as $key => $val) {
"$key = $val\n";

L'exemple ci-dessus va afficher :

a = orange
d = lemon
b = banana
c = apple

Les fruits ont été triés en ordre alphabétique inverse, et leurs index respectifs ont été conservés.

Voir aussi

add a note add a note

User Contributed Notes 5 notes

morgan at anomalyinc dot com
16 years ago
If you need to sort a multi-demension array, for example, an array such as


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...
4 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.

($x);   // optional.
$key_of_max = key($x);   // returns the index.
jordancdarwin at googlemail dot com
8 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);

If successful, will return 1, and error if there is a string used. Useful to note so then people stop asking me :D
4 years ago
If you are dealing with a multidimensional array you want to sort, then this might be helpfull:

function array_sort($arr){
$arr)) return $arr;
$arr as $k => $a){
arsort($arr); // could be any kind of sort
return $arr;
$arr[$k] = array_sort($a);
rodders_plonker at yahoo dot com
16 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 in the notes at
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 :).

To Top