Sorting Array Values in PHP(數(shù)組排序)
更新時(shí)間:2011年09月15日 21:43:14 作者:
有時(shí)候,你可能需要對(duì)數(shù)組內(nèi)的值進(jìn)行排序,那么就可以參考下面的文章。
復(fù)制代碼 代碼如下:
$full_name = array();
$full_name["Roger"] = "Waters";
$full_name["Richard"] = "Wright";
$full_name["Nick"] = "Mason";
$full_name["David"] = "Gilmour";
To sort this array, you just use the assort( ) function. This involves nothing more complex than typing the word asort, followed by round brackets. In between the round brackets, type in the name of your Associative array:
復(fù)制代碼 代碼如下:
asort($full_name);
The letter "a" tells PHP that the array is an Associative one. (If you don't have the "a" before "sort", your key names will turn in to numbers!). The "a" also tells PHP to sort by the Value, and NOT by the key. In our script above, the surnames will be sorted. If you want to sort using the Key, then you can use ksort() instead.
If you have a Scalar array (numbers as Keys), then you leave the "a" off. Like this:
復(fù)制代碼 代碼如下:
$numbers = array( );
$numbers[]="2";
$numbers[]="8";
$numbers[]="10";
$numbers[]="6";
sort($numbers);
print $numbers[0] ;
print $numbers[1];
print $numbers[2] ;
print $numbers[3];
The numbers are then sorted from lowest to highest. If you want to sort in reverse order then you need the following:
rsort( ) – Sorts a Scalar array in reverse order
arsort( ) - Sorts the Values in an Associative array in reverse order
krsort( ) - Sorts the Keys in an Associative array in reverse order
In the next part, we look at how to get a random value from an array.
您可能感興趣的文章:
- php中array_multisort對(duì)多維數(shù)組排序的方法
- php專用數(shù)組排序類ArraySortUtil用法實(shí)例
- php二維數(shù)組排序方法(array_multisort usort)
- php 數(shù)組排序 array_multisort與uasort的區(qū)別
- array_multisort實(shí)現(xiàn)PHP多維數(shù)組排序示例講解
- php關(guān)于array_multisort多維數(shù)組排序的使用說明
- php array_map array_multisort 高效處理多維數(shù)組排序
- PHP多維數(shù)組排序array詳解
相關(guān)文章
php中cookie實(shí)現(xiàn)二級(jí)域名可訪問操作的方法
這篇文章主要介紹了php中cookie實(shí)現(xiàn)二級(jí)域名可訪問操作的方法,對(duì)比了常用的setcookie函數(shù)用法,并給出了一個(gè)設(shè)置cookie的類文件來實(shí)現(xiàn)這一功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11
php+xml實(shí)現(xiàn)在線英文詞典之添加詞條的方法
這篇文章主要介紹了php+xml實(shí)現(xiàn)在線英文詞典之添加詞條的方法,接著上一篇的通過英文查詢漢字進(jìn)一步完善了詞條的添加功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
PHP如何獲取Cookie并實(shí)現(xiàn)模擬登錄
這篇文章主要介紹了PHP如何獲取Cookie并實(shí)現(xiàn)模擬登錄,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
php和C#的yield迭代器實(shí)現(xiàn)方法對(duì)比分析
這篇文章主要介紹了php和C#的yield迭代器實(shí)現(xiàn)方法,簡(jiǎn)單說明了yield迭代器的原理,并結(jié)合具體實(shí)例形式對(duì)比分析了php和C#的yield迭代器相關(guān)使用技巧,需要的朋友可以參考下2019-07-07

