PHP - natsort() 函式



語法

natsort ( $array );

定義和用法

此函式實現了一種排序演算法,該演算法以人類的方式對字母數字字串進行排序,同時保持鍵/值關聯。這被稱為“自然排序”。下面可以看出此演算法與常規計算機字串排序演算法(在 sort() 中使用)之間的區別:

引數

序號 引數和描述
1

array(必填)

指定一個數組

返回值

此函式在成功時返回 TRUE,失敗時返回 FALSE。

示例

嘗試以下示例:

<?php
   $input1 = array('click.txt', 'img12.txt', 'img10.txt', 'img2.txt', 'img1.txt', 'IMG3.txt');
   $input2 = $input1;  
   natsort($input2);
   echo " \n Natural order sorting \n";
   print_r($input2);
?> 

這將產生以下結果:

Natural order sorting
Array ( [0] => click.txt [5] => IMG3.txt [4] => img1.txt [3] => img2.txt [2] => img10.txt [1] => img12.txt )
php_function_reference.htm
廣告