PHP 8.4.0 RC3 available for testing

arsort

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

arsort Ordina un array in ordine decrescente e mantiene le associazioni degli indici

Descrizione

arsort(array $array, int $sort_flags = ?): bool

Questa funzione ordina un array in modo tale che i suoi indici mantengano la loro correlazione con gli elementi ai quali sono associati. Viene usata principalmente nell'ordinamento degli array associativi, quando la disposizione originaria degli elementi è importante.

Restituisce true in caso di successo, false in caso di fallimento.

Example #1 esempio di arsort()

<?php
$frutta
= array("d" => "limone", "a" => "arancia", "b" => "banana", "c" => "mela");
arsort($frutta);
reset($frutta);
while (list(
$chiave, $valore) = each($frutta)) {
echo
"$chiave = $valore\n";
}
?>

Questo esempio mostrerà:

c = mela
d = limone
b = banana
a = arancia

I frutti sono ordinati in ordine alfabetico decrescente, e l'indice associato a ogni elemento è stato mantenuto.

È possibile modificare il comportamento dell'ordinamento usando il parametro opzionale sort_flags, per maggiori dettagli vedere sort().

vedere anche asort(), rsort(), ksort() e sort().

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