natcasesort

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

natcasesort Ordina un array usando un algoritmo di "ordine naturale" non sensibile alle maiuscole/minuscole

Descrizione

natcasesort(array $array): void

Questa funziona implementa un algoritmo di ordinamento che ordina le stringhe alfanumeriche come lo farebbe un essere umano, mantenendo le associazioni chiavi/valori. Questo è chiamato "ordine naturale".

natcasesort() è una versione, non sensibile alle maiuscole/minuscole, di natsort().

Example #1 esempio di natcasesort()

<?php
$array1
= $array2 = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');

sort($array1);
echo
"Ordinamento standard\n";
print_r($array1);

natcasesort($array2);
echo
"\nOrdinamento naturale (con maiuscole non significative)\n";
print_r($array2);
?>

Questo codice genererà il seguente risultato:

Ordinamento standard
Array
(
    [0] => IMG0.png
    [1] => IMG3.png
    [2] => img1.png
    [3] => img10.png
    [4] => img12.png
    [5] => img2.png
)

Ordinamento naturale (con maiuscole non significative)
Array
(
    [0] => IMG0.png
    [4] => img1.png
    [3] => img2.png
    [5] => IMG3.png
    [2] => img10.png
    [1] => img12.png
)

Per maggiori informazioni vedere la pagina di Martin Pool » Natural Order String Comparison .

Vedere anche sort(), natsort(), strnatcmp() e strnatcasecmp().

add a note

User Contributed Notes 1 note

up
46
dslicer at maine dot rr dot com
22 years ago
Something that should probably be documented is the fact that both natsort and natcasesort maintain the key-value associations of the array. If you natsort a numerically indexed array, a for loop will not produce the sorted order; a foreach loop, however, will produce the sorted order, but the indices won't be in numeric order. If you want natsort and natcasesort to break the key-value associations, just use array_values on the sorted array, like so:

natcasesort($arr);
$arr = array_values($arr);
To Top