(PHP 4, PHP 5, PHP 7, PHP 8)
arsort — Ordena un array en orden descendente y conserva la asociación de los índices
Ordena array en el lugar en orden descendente,
de tal manera que la correlación entre las claves y
los valores se conserve.
El uso principal es cuando se ordenan arrays asociativos donde el orden de los elementos es importante.
Nota: Si dos miembros se comparan como iguales, mantienen su orden original. Anterior a PHP 8.0.0, su orden relativo en el array ordenado no está definido.
Nota: Reinicia el puntero interno del array al primer elemento.
arrayEl array de entrada.
flagsflags
puede ser utilizado para modificar el comportamiento de ordenación utilizando estos valores:
Tipo de banderas de ordenación:
SORT_REGULAR - compara los elementos normalmente; los
detalles son descritos en la sección de los operadores de comparación
SORT_NUMERIC - compara los elementos numéricamente
SORT_STRING - compara los elementos como strings
SORT_LOCALE_STRING - compara los elementos como
strings, basado en la configuración regional actual. Esto utiliza la configuración
regional, que puede ser cambiada utilizando setlocale()
SORT_NATURAL - compara los elementos como strings
utilizando el "orden natural" como natsort()
SORT_FLAG_CASE - puede ser combinado (OR a
nivel de bits) con
SORT_STRING o
SORT_NATURAL para ordenar strings sin tener en cuenta la mayúscula/minúscula
Retorna siempre true .
| Versión | Descripción |
|---|---|
| 8.2.0 |
El tipo de retorno es ahora true , anteriormente era bool .
|
Ejemplo #1 Ejemplo con arsort()
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
arsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>El ejemplo anterior mostrará:
a = orange d = lemon b = banana c = apple
Las frutas han sido ordenadas en orden alfabético inverso, y sus índices respectivos han sido conservados.
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...
-moI 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 )