natcasesort

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

natcasesortSort an array using a case insensitive "natural order" algorithm

Опис

natcasesort(array &$array): true

natcasesort() is a case insensitive version of natsort().

This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering".

Зауваження:

Якщо два члени порівняння однакові, їхній порядок зберігається. До PHP 8.0.0, їхній відносний порядок у впорядкованому масиві був невизначеним.

Зауваження:

Скидає внутрішній вказівник масиву на перший елемент.

Параметри

array

The input array.

Значення, що повертаються

Завжди повертає true.

Журнал змін

Версія Опис
8.2.0 Тепер повертається значення типу true. До цього був bool.

Приклади

Приклад #1 natcasesort() example

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

sort($array1);
echo
"Standard sorting\n";
print_r($array1);

natcasesort($array2);
echo
"\nNatural order sorting (case-insensitive)\n";
print_r($array2);
?>

Поданий вище приклад виведе:

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

Natural order sorting (case-insensitive)
Array
(
    [0] => IMG0.png
    [4] => img1.png
    [3] => img2.png
    [5] => IMG3.png
    [2] => img10.png
    [1] => img12.png
)

For more information see: Martin Pool's » Natural Order String Comparison page.

Прогляньте також

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