PHP natcasesort() 函數

定義和用法

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 "自然排序:";
print_r($temp_files);
echo "<br />";
natcasesort($temp_files);
echo "不區分大小寫的自然排序:";
print_r($temp_files);
?>

以上代碼的輸出:

自然排序:
Array
(
[0] => Temp10.txt
[1] => Temp22.txt
[2] => temp1.txt
[4] => temp2.txt
[3] => temp15.txt
)
不區分大小寫的自然順序:
Array
(
[2] => temp1.txt
[4] => temp2.txt
[0] => Temp10.txt
[3] => temp15.txt
[1] => Temp22.txt
)