• PHP (20140523)


    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  .  "
    " ;
     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 
    " ;
    }
    ?> 
    //以上例程会输出:
    
    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 
    " ;
     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 
    " ;
     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 
    " ;
     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 
    " ;
     6 }
     7 ?> 
     8 //以上例程会输出:
     9 
    10 d = lemon
    11 c = apple
    12 b = banana
    13 a = orange
  • 相关阅读:
    pic32 + Vs1003 正弦测试通过
    c强转问题
    Ucos ii 移植到LPC2148
    Pic32时钟
    linux下的文件扫描程序(转载)
    pic32 spi驱动pmodcls lcd液晶屏
    最*不太*
    vhdl元件例化语句
    被忽略了的gcc 浮点选项
    Linux C/S文件传输和云端文件下载服务模拟
  • 原文地址:https://www.cnblogs.com/sunshine-c/p/3748488.html
Copyright © 2020-2023  润新知