zoukankan      html  css  js  c++  java
  • php array数组(第一部分)

    创建一个数组
    <?php
        $arr = array("My","name","is","zhangsan");
        echo $arr[0]." ".$arr[1]." ".$arr[2]." ".$arr[3];
    ?>
    
    //输出
    My name is zhangsan
    
    array_change_key_case() 将数组的所有键值转换为大写

    参数 CASE_UPPER 转换为大写
    参数 CASE_LOWER 转换为小写

    <?php
       $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
       print_r(array_change_key_case($age,CASE_UPPER));
    ?>
    
    //输出
    Array ( [PETER] => 35 [BEN] => 37 [JOE] => 43 )
    
    array_chunk() 把一个数组分割为新的数组块
    <?php
        $cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
        print_r(array_chunk($cars,2));
    ?>
    
    //输出
    Array
    (
        [0] => Array
            (
                [0] => Volvo
                [1] => BMW
            )
    
        [1] => Array
            (
                [0] => Toyota
                [1] => Honda
            )
    
        [2] => Array
            (
                [0] => Mercedes
                [1] => Opel
            )
    
    )
    
    array_column() 返回输入数组中某个单一列的值
    <?php
    // 可能从数据库中返回数组
    $a = array(
      array(
        'id' => 5698,
        'first_name' => 'Peter',
        'last_name' => 'Griffin',
      ),
      array(
        'id' => 4767,
        'first_name' => 'Ben',
        'last_name' => 'Smith',
      ),
      array(
        'id' => 3809,
        'first_name' => 'Joe',
        'last_name' => 'Doe',
      )
    );
    
    $last_names = array_column($a, 'last_name');
    print_r($last_names);
    ?>
    
    //输出
    Array
    (
      [0] => Griffin
      [1] => Smith
      [2] => Doe
    )
    
    array_combine() 通过合并两个数组来创建一个新数组,其中的一个数组元素为键名,另一个数组的元素为键值。
    <?php
        $fname=array("Peter","Ben","Joe");
        $age=array("35","37","43");
        $c=array_combine($fname,$age);
        print_r($c);
    ?>
    
    //输出
    Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
    
    array_count_values() 统计数组中所有值出现的次数
    <?php
        $a=array("A","Cat","Dog","A","Dog");
        print_r(array_count_values($a));
    ?>
    
    //输出
    Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
    
    array_diff() 用于比较两个(或更多个)数组的键值,并返回差集(只比较键值)
    <?php
        $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
        $a2=array("e"=>"red","f"=>"green","g"=>"blue");
        
        $result=array_diff($a1,$a2);
        print_r($result);
    ?>
    
    //输出
    Array ( [d] => yellow )
    
    array_diff_assoc() 用于比较两个(或更多个)数组的键名和键值,并返回差集。
    <?php
        $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
        $a2=array("a"=>"red","b"=>"green","c"=>"blue");
        
        $result=array_diff_assoc($a1,$a2);
        print_r($result);
    ?>
    
    //输出
    Array ( [d] => yellow )
    
    
    <?php
        $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
        $a2=array("e"=>"red","f"=>"green","g"=>"blue");
        
        $result=array_diff_assoc($a1,$a2);
        print_r($result);
    ?>
    
    //输出
    Array ( [a] => red [b] => green [c] => blue [d] => yellow )
    
    array_diff_key() 用于比较两个(或更多个)数组的键名,并返回差集
    <?php
        $a1=array("a"=>"red","b"=>"green","c"=>"blue");
        $a2=array("a"=>"red","c"=>"blue","d"=>"pink");
        
        $result=array_diff_key($a1,$a2);
        print_r($result);
    ?>
    //输出
    Array ( [b] => green )
    
    array_diff_uassoc() 用于比较两个(或更多个)数组的键名和键值,并返回差集(该函数使用用户自定义函数比较键名和键值)。
    <?php
    function myfunction($a,$b)
    {
        if ($a===$b)
        {
           return 0;
        }
        return ($a>$b)?1:-1;
    }
    
    $a1=array("a"=>"red","b"=>"green","c"=>"blue");
    $a2=array("d"=>"red","b"=>"green","e"=>"blue");
    
    $result=array_diff_uassoc($a1,$a2,"myfunction");
    print_r($result);
    ?>
    
    //输出
    Array ( [a] => red [c] => blue )
    
    array_diff_ukey() 用于比较两个(或更多)数组的键名,并返回差集(该函数使用用户自定义函数比较键名)
    <?php
    function myfunction($a,$b)
    {
       if ($a===$b)
       {
          return 0;
       }
       return ($a>$b)?1:-1;
    }
    
    $a1=array("a"=>"red","b"=>"green","c"=>"blue");
    $a2=array("a"=>"blue","b"=>"black","e"=>"blue");
    
    $result=array_diff_ukey($a1,$a2,"myfunction");
    print_r($result);
    ?>
    
    //输出
    Array ( [c] => blue )
    
  • 相关阅读:
    UEditor 编辑模板
    Task ProgressBar模拟现实完成后显示TextBox
    Java Lambda map返回部分属性
    Socket编程
    字节流和字符流
    File类
    volatile的作用和原理
    Java四种引用类型
    ThreadLocal
    孤儿进程和僵尸进程
  • 原文地址:https://www.cnblogs.com/yxhblogs/p/6747154.html
Copyright © 2011-2022 走看看