Funkcja PHP natcasesort()
Przykład
Sortuj tablicę za pomocą natsort() i natcasesort():
<?php
$temp_files = array("temp15.txt","Temp10.txt",
"temp1.txt","Temp22.txt","temp2.txt");
natsort($temp_files);
echo "Natural order: ";
print_r($temp_files);
echo "<br />";
natcasesort($temp_files);
echo "Natural order case insensitve: ";
print_r($temp_files);
?>
Wynikiem powyższego kodu będzie:
Natural order:
Array
(
[0] => Temp10.txt
[1] => Temp22.txt
[2] => temp1.txt
[4] => temp2.txt
[3] => temp15.txt
)
Natural order case insensitve:
Array
(
[2] => temp1.txt
[4] => temp2.txt
[0] => Temp10.txt
[3] => temp15.txt
[1] => Temp22.txt
)
Definicja i użycie
Funkcja natcasesort() sortuje tablicę przy użyciu algorytmu „porządku naturalnego”. Wartości zachowują swoje oryginalne klucze.
W naturalnym algorytmie liczba 2 jest mniejsza niż liczba 10. W sortowaniu komputerowym 10 jest mniejsze niż 2, ponieważ pierwsza liczba w „10” jest mniejsza niż 2.
W tej funkcji nie jest rozróżniana wielkość liter.
Składnia
natcasesort(array)
Wartości parametrów
Parameter | Description |
---|---|
array | Required. Specifies the array to sort |
Szczegóły techniczne
Wartość zwrotu: | PRAWDA o sukcesie. FAŁSZ w przypadku niepowodzenia |
---|---|
Wersja PHP: | 4+ |
❮ Odniesienie do tablicy PHP