sort();
升序
对数组排序;
1 1 <?php
2 2
3 3 $fruits = array( "lemon" , "orange" , "banana" , "apple" );
4 4 sort ( $fruits );
5 5 foreach ( $fruits as $key => $val ) {
6 6 echo "fruits[" . $key . "] = " . $val . "\n" ;
7 7 }
8 8
9 9 ?>
10 10 //输出结果
11 11
12 12 fruits[0] = apple
13 13 fruits[1] = banana
14 14 fruits[2] = lemon
15 15 fruits[3] = orange
rsort();降序
-对数组逆向排序
此函数为 array
中的元素赋与新的键名。这将删除原有的键名,而不是仅仅将键名重新排序。
<?php
$fruits = array( "lemon" , "orange" , "banana" , "apple" );
rsort ( $fruits );
foreach ( $fruits as $key => $val ) {
echo " $key = $val \n" ;
}
?>
//以上例程会输出:0 = orange
1 = lemon
2 = banana
3 = apple
arsort();升序
对数组进行逆向排序并保持索引关系
1 <?php
2 $fruits = array( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
3 arsort ( $fruits );
4 foreach ( $fruits as $key => $val ) {
5 echo " $key = $val \n" ;
6 }
7 ?>
8 //以上例程会输出:
9
10 a = orange
11 d = lemon
12 b = banana
13 c = apple
asort();降序
对数组进行排序并保持索引关系;排值。
1 <?php
2 $fruits = array( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
3 asort ( $fruits );
4 foreach ( $fruits as $key => $val ) {
5 echo " $key = $val \n" ;
6 }
7 ?>
8 //以上例程会输出:
9
10 c = apple
11 b = banana
12 d = lemon
13 a = orange
ksort();升序
对数组按照键名排序。
1 <?php
2 $fruits = array( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
3 ksort ( $fruits );
4 foreach ( $fruits as $key => $val ) {
5 echo " $key = $val \n" ;
6 }
7 ?>
8 //以上例程会输出:
9
10 a = orange
11 b = banana
12 c = apple
13 d = lemon
krsort ();降序
对数组按照键名逆向排序
1 <?php
2 $fruits = array( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" );
3 krsort ( $fruits );
4 foreach ( $fruits as $key => $val ) {
5 echo " $key = $val \n" ;
6 }
7 ?>
8 //以上例程会输出:
9
10 d = lemon
11 c = apple
12 b = banana
13 a = orange
PHP (20140523)
时间: 2024-10-11 22:56:02