首页 > Web开发 > 详细

PHP (20140523)

时间:2014-05-24 10:07:57      阅读:379      评论:0      收藏:0      [点我收藏+]

sort();  升序

 对数组排序;

bubuko.com,布布扣
 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
bubuko.com,布布扣

 

rsort();降序

-对数组逆向排序

此函数为 array 中的元素赋与新的键名。这将删除原有的键名,而不是仅仅将键名重新排序。

bubuko.com,布布扣
<?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
bubuko.com,布布扣

 

 

arsort();升序

对数组进行逆向排序并保持索引关系

 

bubuko.com,布布扣
 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
bubuko.com,布布扣

 

asort();降序

对数组进行排序并保持索引关系;排值。

bubuko.com,布布扣
 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
bubuko.com,布布扣

 

ksort();升序

对数组按照键名排序。

bubuko.com,布布扣
 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
bubuko.com,布布扣

 

krsort ();降序

 对数组按照键名逆向排序

 

bubuko.com,布布扣
 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
bubuko.com,布布扣

 

PHP (20140523),布布扣,bubuko.com

PHP (20140523)

原文:http://www.cnblogs.com/sunshine-c/p/3748488.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!