• php array


    1、创建数组赋值的几种方法

    (1)$addresses[0] = "spam@cyberpromo.net";
         $addresses[1] = "abuse@example.com"
         $addresses[2] = "root@example.com"
    (2)$addresses = array("spam@cyberpromo.net","abuse@example.com", "root@example.com");
    (3)$price = array(
        'gasket' => 15.29,
        'wheel'  => 75.25,
        'tire'       =>50.00
        );
    (4)$days = [ 'gasket' => 15.29, 'wheel'  => 75.25,  'tire'       =>50.00];
    (5)创建空数组
        $addresses = array();
    (6)$days = array(1 => "Mon","Tue","Wed");
    2、在数组末尾添加值
        $family = array("Fred","Wilma");
        $family[] = "Fred";
    3、指定值的范围
        $numbers = range(2,5);   
        $letters = range('a','z');
    4、获得数组大小
        $days = [ 'gasket' => 15.29, 'wheel'  => 75.25,  'tire'       =>50.00];
        $size = count($days);
        $size =sizeof($days);
    5、填充数组
        $padded = array_pad($scores,5,0);   分别为 数组 ,元素个数,要用的填充值
    6、多维数组
        $row0 = array(1,2,3);
        $row1 = array(4,5,6);
        $row2 = array(7,8,9);
        $multi  = array($row0,$row1,$row2);
        $value = $multi[2][0];
    7、析取多个值
        $person = array("Fred",35,"Betty");
        list($name,$age,$wife) = $person;
    8、切割数组
        $subset = array_slice(array,offset,length);        //offset为要复制的初始元素,length为要复制的个数。
    9、数组分块
        $chunk = array_chunk(array,size,[,preserve_keys]);   //size为每块的大小,从前向后分块,当不足一块的时候自动划分为一块。
    10、返回数组中的键
        $arrayOfValues = array_keys(array);
    11、检出元素是否存在
        if( array_key_exists(key,array)) { ......}
    12、在数组中删除和插入元素
        删除:$removed = array_splice(array,start[, length [ , replacement ] ]);    //最后一个参数不需要
                  $removed = array_splice($subjects,2,3);
        插入元素:$new = array("law","business","IS");
                          array_splice($subjects,2,0,$new);
    13、数组和变量之间的转换
        数组->变量 : extract($array,EXTR_PREFIX_ALL,"book");   第二第三个指定要创建变量名的前缀
        变量->数组 :$a = compact("color","shape",floppy");
    14、遍历数组
        foreach: 
            $addresses = array("spam@cyberpromo.net","abuse@example.com", "root@example.com");
            foreach ($addresses as $value) {
                echo "Processing {$value} ";
            }
        迭代器:
        current()   reset()    next()    prev()    end()    each()    key()    
        for循环:
        array_walk()
        array_reduce()
    15、查找元素
        $addresses = array("spam@cyberpromo.net","abuse@example.com", "root@example.com");
        $gotSpam = in_array("spam@cyberpromo.net",$addresses);
    16、排序
        一次对多个数组进行排序
        array_multisort($ages,SORT_ASC,$zips,SORT_DESC,$names,SORT_ASC);
    17、翻转数组
        $addresses = array("spam@cyberpromo.net","abuse@example.com", "root@example.com");
        $sesserdda = array_reverse(array);
    18、随机排序
        shuffle()
    19、计算数组的值
        $sum = array_sum($count);
    20、合并数组
        $first = array();
        $second = array();
        $merged = array_merge($first,$second);
  • 相关阅读:
    LVM
    作业
    软件工程随笔
    woj1005-holding animals-01pack woj1006-Language of animals-BFS
    woj1002-Genesis woj1003-birthofnoah woj1004-noah's ark
    C++ new delete malloc free
    centos7+腾讯云服务器搭建wordpress
    C++ inline与operator
    next v5升级到next v7需要注意的地方
    python = 赋值顺序 && C++ side effect
  • 原文地址:https://www.cnblogs.com/wddx/p/5408083.html
Copyright © 2020-2023  润新知