PHP natsort() 函數(shù)
定義和用法
natsort() 函數(shù)用自然順序算法對給定數(shù)組中的元素排序。
natsort() 函數(shù)實現(xiàn)了“自然排序”,即數(shù)字從 1 到 9 的排序方法,字母從 a 到 z 的排序方法,短者優(yōu)先。數(shù)組的索引與單元值保持關(guān)聯(lián)。
如果成功,則該函數(shù)返回 TRUE,否則返回 FALSE。
語法
natsort(array)
參數(shù) | 描述 |
---|---|
array | 必需。規(guī)定要進行排序的數(shù)組。 |
例子
本函數(shù)所用的自然排序算法,與通常的計算機字符串排序算法(用于 sort())的區(qū)別,見下面示例:
<?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); ?>
輸出:
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 )