当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > natsort() 函数
The natsort() function sorts an array by using a "natural order" algorithm. The values keep their original keys.
natsort()函数的作用是用“自然排序”算法对数组排序。数组内所有值的键保持不变。
In a natural algorithm, the number 2 is less than the number 10. In computer sorting, 10 is less than 2, because the first number in "10" is less than 2.
在“自然运算法则[natural algorithm]”中,“2”小于“10”;在计算机排序运算法则中,“10”是小于“2”的,因为“10”的第一个数字“1”小于“2”。
This function returns TRUE on success, or FALSE on failure.
如果成功,则该函数返回True;失败则返回False。
natsort(array) |
Parameter 参数 | Description 描述 |
---|---|
array | Required. Specifies the array to sort 必要函数。指定需要进行排序的数组 |
<?php $temp_files = array("temp15.txt","temp10.txt", "temp1.txt","temp22.txt","temp2.txt"); sort($temp_files); echo "Standard sorting: "; print_r($temp_files); echo "<br />"; natsort($temp_files); echo "Natural order: "; print_r($temp_files); ?> |
The output of the code above will be:
上述代码将输出下面的结果:
Standard sorting: Array ( [0] => temp1.txt [1] => temp10.txt [2] => temp15.txt [3] => temp2.txt [4] => temp22.txt ) Natural order: Array ( [0] => temp1.txt [3] => temp2.txt [1] => temp10.txt [2] => temp15.txt [4] => temp22.txt ) |