CKTIS / PHP
ksort()
Dispone le chiavi in ordine crescente prima di scorrere la mappa.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
ksort(array &$array, int $flags = SORT_REGULAR): trueDispone le chiavi in ordine crescente prima di scorrere la mappa. Per 10 e 2, SORT_STRING e SORT_NUMERIC producono ordini diversi. I valori restano associati, l’argomento cambia e la funzione restituisce true. Da PHP 8.2 il modo predefinito SORT_REGULAR confronta le chiavi "09" e "010" numericamente e mette "09" per prima. SORT_STRING mette "010" per prima.
Firma
ksort(array &$array, int $flags = SORT_REGULAR): true
Output dell’esempio
[[10,2],[2,10]]
[["09","010"],["010","09"]]
Esempi
<?php
$rows = [10=>"ten",2=>"two"];
$text = $rows; ksort($text, SORT_STRING);
ksort($rows,SORT_NUMERIC);
echo json_encode([array_keys($text),array_keys($rows)]);
$padded=["09"=>9,"010"=>10]; $lexical=$padded;
ksort($padded); ksort($lexical,SORT_STRING);
echo PHP_EOL,json_encode([array_keys($padded),array_keys($lexical)]);
Risultato
[[10,2],[2,10]] [["09","010"],["010","09"]]