国产探花免费观看_亚洲丰满少妇自慰呻吟_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.
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鹿邑县| 青川县| 龙里县| 通榆县| 界首市| 屯昌县| 策勒县| 策勒县| 宝兴县| 卓尼县| 崇义县| 洛宁县| 光山县| 平潭县| 晋城| 壤塘县| 宝山区| 丰原市| 佛山市| 锡林浩特市| 澄江县| 开原市| 宜章县| 甘泉县| 射阳县| 东兴市| 昆明市| 遂宁市| 老河口市| 牟定县| 始兴县| 金山区| 东城区| 吉木乃县| 高陵县| 永德县| 平山县| 泸州市| 利辛县| 雅安市| 云南省|