PHP natcasesort() 函数


定义和用法

natcasesort() 函数用"自然排序"算法对数组进行排序。键值保留它们原始的键名。

在自然排序算法中,数字 2 小于 数字 10。在计算机排序算法中,10 小于 2,因为 "10" 中的第一个数字小于 2。

该函数不区分大小写。

如果成功,该函数返回 TRUE,如果失败则返回 FALSE。

语法

  1. natcasesort(array)

参数 描述
array 必需。规定要进行排序的数组。

实例

  1. <?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);
    ?>

上面代码将输出:

  1. 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
    )