PHP natcasesort() 函数

2025 年 1 月 6 日 | 阅读 2 分钟

natcasesort( ) 函数用于使用不区分大小写的“自然排序”算法对数组进行排序。该函数实现了一种排序算法,但保留了原始的键和值。该函数在 PHP 4.0 中引入。

语法

参数

参数描述必需
数组输入的数组。必需

返回值

natcasesort( ) 函数成功时返回 true,失败时返回 false。

示例 1

输出

Standard sorting
Array
(
    [0] => Rahul
    [1] => Sachin
    [2] =>sehwag
    [3] =>virat
)
Natural order case insensitve: Array
(
    [2] => Rahul
    [0] => Sachin
    [1] =>sehwag
    [3] =>virat
)

示例 2

输出

Standard sorting
Array
(
    [0] => Code1.php
    [1] => Code22.txt
    [2] => code12.php
    [3] => code2.php
    [4] => code3.php
)
Natural order case insensitve: Array
(
    [4] => Code1.php
    [2] => code2.php
    [3] => code3.php
    [0] => code12.php
    [1] => Code22.txt
)

示例 3

输出

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

示例 4

输出

Standard sorting
Array
(
    [0] => Java
    [1] => Python
    [2] => Swift
    [3] =>php
)
Natural order case insensitve: Array
(
    [0] => Java
    [1] =>php
    [3] => Python
    [2] => Swift
)