国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

Sorting Array Values in PHP(數組排序)

2024-05-04 23:17:03
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


$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:

復制代碼 代碼如下:


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:

復制代碼 代碼如下:


$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.
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎赉特旗| 屯昌县| 扶沟县| 确山县| 洪泽县| 和平县| 新乡县| 边坝县| 福清市| 晴隆县| 永和县| 浦城县| 集安市| 北川| 柯坪县| 西安市| 吴旗县| 洞口县| 榆树市| 化州市| 龙游县| 东莞市| 布尔津县| 新巴尔虎右旗| 靖安县| 天水市| 育儿| 隆安县| 应城市| 聊城市| 通许县| 霍邱县| 哈尔滨市| 安阳市| 肃南| 上虞市| 松潘县| 简阳市| 德格县| 怀化市| 沈丘县|