PHP開發筆記系列(九)- 數組(三)
??? 寫了兩篇關於Php數組的日常使用,本篇《PHP開發筆記系列(九)- 數組(三)》,主要關注數組的大小和排序。
??? 1. 擷取數組長度
??? count()函數返回數組中的元素個數,是sizeof()是count()的別名,功能一樣。
?
file: count.phpurl: http://localhost:88/array/count.php
?
??? count()函數除了接受第一個被count的陣列變數外,還可以接受一個mode的參數,指定是否需要進行遞迴,統計多維陣列中所有元素的個數,代碼如下:
?
file: count.phpurl: http://localhost:88/array/count.php
?
??? 2. 統計數組元素出現頻率
??? array_count_values()函數返回一個包含關聯鍵/值的數組。如'A'出現2次,'B'出現1次,之類。代碼如下:
?
file: array_count_values.phpurl: http://localhost:88/array/array_count_values.php====================
"; $favor1 = array('1'=>'Sport', '2'=>'Sing', '3'=>'Sing', '4'=>'Travel'); $stats1 = array_count_values($favor1); print_r($stats1); ?>
?
??? 3. 返回數組非重複元素
??? array_unique()函數會刪除數組中所有重複的值,返回一個由不重複值組成的數組,原數組元素不變,代碼如下:
?
file: array_unique.phpurl: http://localhost:88/array/array_unique.php====================
"; print_r($unique); ?>
?
??? 4. 數組排序
??? sort()函數對數組進行排序,各元素按值由低到高的順序排列。sort()函數不反悔排序後的數組,相反它只是排序,不論結果如何都不反悔任何值。同時,可以為排序指定定序,如SORT_NUMERIC是按數值排序,應用在整數和浮點數上,SORT_REGULAR是按ASCII值進行排序,應用在字元或字串上,SORT_STRING是按字串排序。代碼如下:
??
??? 注意:sort()函數會直接修改被傳入的array中的值,仔細查看下面程式的輸出結果。
?
file: sort.phpurl: http://localhost:88/array/sort.php'; sort($number, SORT_NUMERIC); print_r($number); echo "
====================
"; $letter = array('a', 'A', 'Z', 'f', 'G', 'e'); print_r($letter); echo '
'; // 預設是模式是SORT_REGULAR sort($letter, SORT_REGULAR); print_r($letter); echo "
====================
"; $string = array('Jack', 'Mike', 'Mary', 'Jassica', 'Ruby'); print_r($string); echo '
'; sort($string, SORT_STRING); print_r($string); ?>
?
??? 從上面的結果可以查看,sort()函數修改的不僅被傳入數組的元素,而且把鍵與值的對應關係都改變了,若需要保持鍵/值對的關係,需要用到asort()函數,如下:
?
file: asort.phpurl: http://localhost:88/array/asort.php'; asort($number, SORT_NUMERIC); print_r($number); echo "
====================
"; $letter = array('a', 'A', 'Z', 'f', 'G', 'e'); print_r($letter); echo '
'; // 預設是模式是SORT_REGULAR asort($letter, SORT_REGULAR); print_r($letter); echo "
====================
"; $string = array('Jack', 'Mike', 'Mary', 'Jassica', 'Ruby'); print_r($string); echo '
'; asort($string, SORT_STRING); print_r($string); ?>
?
??? 5. 逆序排序
??? rsort()函數與sort函數類似,同樣有保持索引值對關係和不保持索引值對關係的模式,使用方法一致。
??? 6. 數組自然排序
??? 在數組的排序中,如student1.jpg, student2.jpg,student10.jpg, student20.jgp,使用典型的排序如sort後,會排出student1.jpg, studdent10.jpg, student2.jpg, student20.jpg這樣的順序,但期望的順序是student1.jpg, student2.jpg, studdent10.jpg, student20.jpg。這時就需要用到natsort()函數了。
?
file: natsort.phpurl: http://localhost:88/array/natsort.php ==========================
"; sort($student); print_r($student); echo "
==========================
"; natsort($student); print_r($student); ?>
?
??? natsort()函數還有一個變形,叫natcasesort()函數,在功能上與natsort()函數相同,只是不缺分大小寫。
?
file: natcasesort.phpurl: http://localhost:88/array/natcasesort.php
?
??? 7. 按索引值對數組排序
??? 上述函數主要對數值鍵數組進行排序,那麼對關聯鍵數組進行排序的話,可能有特殊的需求,例如按照索引值進行排序,此時需要用到ksort()函數,成功返回TRUE,失敗返回FALSE。同時還象sort()函數一樣,接受一個sort_flags的參數,為SORT_NUMERIC,SORT_REGULAR,SORT_STRING,指定排序模式。代碼如下:
?
file: ksort.phpurl: http://localhost:88/array/ksort.php ==========================
"; ksort($student); print_r($student); ?>
?
??? 8. 自訂定序
??? Php還提供了一個自定排序的方法,就是usort(),可以使用在該函數中指定的使用者自訂比較演算法對數組排序。但是一般用得較少,大家自己實驗一下。
???
??? Php數組的排序部分就講到這裡,下次繼續Php數組的後續內容。
??? 本文地址:http://ryan-d.iteye.com/blog/1566686
?
?