PHP 教程 PHP natcasesort() 函数

jeffrey · 2022-03-06 21:56:58 · 热度: 4

定义和用法

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

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

该函数不区分大小写。

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

语法

natcasesort(array)

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

实例

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

上面代码将输出:

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
)

查看更多 PHP Array 函数

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册