您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Sorting Array Values in PHP

2024/4/6 23:32:57发布12次查看
there may be times when you want to sort the values inside of an array. for example, suppose your array values are not in alphabetical order. like this one:
$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.
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product