Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

krsort()

Ordina la mappa per chiavi decrescenti mantenendo i valori associati.

Versione: PHP 8.4; repository runtime, production runtime not attested

Sintassi

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

Ordina la mappa per chiavi decrescenti mantenendo i valori associati. SORT_NUMERIC mette per prima la chiave numerica maggiore. Restituisce true e conserva le chiavi non consecutive nell’array modificato. Da PHP 8.2 il modo predefinito SORT_REGULAR confronta numericamente le chiavi testuali numeriche. In ordine decrescente "010" precede "09", al contrario con SORT_STRING.

Firma

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

Output dell’esempio

[true,[11,5,2],["eleven","five","two"]]
[["010","09"],["09","010"]]

Esempi

<?php
$rows = [2=>"two",11=>"eleven",5=>"five"];
$ok = krsort($rows,SORT_NUMERIC);
echo json_encode([$ok,array_keys($rows),array_values($rows)]);
$padded=["09"=>9,"010"=>10]; $lexical=$padded;
krsort($padded); krsort($lexical,SORT_STRING);
echo PHP_EOL,json_encode([array_keys($padded),array_keys($lexical)]);

Risultato

[true,[11,5,2],["eleven","five","two"]]
[["010","09"],["09","010"]]

Fonti