美文网首页
第五章 数组

第五章 数组

作者: 天黑北风吹 | 来源:发表于2016-06-05 15:32 被阅读23次

    1.创建数组 array()[]list()range()

    <?php
    //1.用array()创建
    $array = array(
        "foo" => "bar",
        "bar" => "foo",
    );
    
    //2.用[] 创建
    // 自 PHP 5.4 起
    $array = [
        "foo" => "bar",
        "bar" => "foo",
    ];
    
    //3.用list()提取数组
    $info = array('coffee', 'brown', 'caffeine');
    // 列出所有变量
    $info = array('coffee', 'brown', 'caffeine');
    list($drink, $color, $power) = $info;
    echo "$drink is $color and $power makes it special.\n";
    
    //使用预定义的值范围填充数组range()
    
    $die = range(1, 6);
    //*类似指定$die = array(1, 2, 3, 4, 5, 6);
    
    $even =range(0, 10, 2);
    //$even = array(0, 2, 4, 6, 8, 10);
    
    $letters = range("A", "D");
    //$letters = array("A", "B", "C", "D");
    ?>
    

    2.判断数组 is_array() ;

    boolean is_array(mixed variable)
    

    3.输出数组

    //1.foreach
    $states = array("Ohio", "Florida", "Texas");
    foreach ($states as $state) {
        echo "$state<br />";
    }
    
    //打印数组用于测试
    print_r($states) 
    

    4.添加和删除数组元素(push pop shift unshift)

    //1.在数组头添加元素 array_unshift()
    
    $states = array("Ohio", "New York");
    array_unshift($states, "California", "Texas");
    
    //$states = array("California", "Texas", "Ohio", "New York");
    
    //2.在数组尾添加元素 array_push()
    
    $states = array("Ohio", "New York");
    array_push($states, "California", "Texas");
    
    //$states = array( "Ohio", "New York", "California", "Texas");
    
    //3.从数组头删除元素 array_shift()
    
    $states = array( "Ohio", "New York", "California", "Texas");
    $state = array_shift($states);
    
    //$states = array("New York", "California", "Texas");
    //$state = array( "Ohio");
    
    //4.从数组尾删除元素 array_pop()
    $states = array( "Ohio", "New York", "California", "Texas");
    $state = array_pop($states);
    
    //$states = array("Ohio", "New York", "California");
    //$state = array( "Texas");
    

    5.定位数组元素

    //1.搜索数组 in_array()
    $state = "Ohio";
    $states = array("California", "Hawaii", "Ohio", "New York");
    if(in_array($state, $states)) echo "not to worry, $state is smoke-free";
    
    //2.搜索关联数组键
    $state["Delaware"] = "December 7, 1787";
    $state["Pennsylvania"] = "December 12, 1787";
    $state["Ohio"] = "March 1, 1803";
    
    if(array_key_exists("Ohio", $state))
        printf("Ohio joined the Union on %s", $state["Ohio"]);
    
    //3.搜索关联数组值 array_search()
    
    $state["Ohio"] = "March 1";
    $state["Delaware"] = "December 7";
    $state["Pennsylvania"] = "December 12";
    
    $founded = array_search("December 7", $state);
    
    if($founded)
        printf("%s was founded on %s",$founded ,$state[$founded]);
    
    //4.获取数组键 array_keys()
    
    $state["Ohio"] = "March 1";
    $state["Delaware"] = "December 7";
    $state["Pennsylvania"] = "December 12";
    
    $keys = array_keys($state);
    print_r($keys);
    
    //Array ( [0] => Ohio [1] => Delaware [2] => Pennsylvania )
    
    //4.获取数组值 array_values()
    
    $state["Ohio"] = "March 1";
    $state["Delaware"] = "December 7";
    $state["Pennsylvania"] = "December 12";
    
    $values = array_values($state);
    print_r($values);
    
    //Array ( [0] => March 1 [1] => December 7 [2] => December 12 )
    
    //5.遍历数组
    
    //a.获取当前数组键key()
    //b.获取当前数组值current()
    //c.获取当前数组键和值each()
    
    //6.移动数组指针
    
    //a.将指针移动到下一个数组位置 next()
    //b.将指针移动到前一个数组位置 prev()
    //c.将指针移动到第一个数组位置 reset()
    //d.将指针移动到最后一个数组位置 end()
    
    //7.向函数传递组值 array_walk() 用于表单
    function sanitize_data($value, $key) {
        $value = strip_tags($value);
    }
    
    array_walk($_POST['keyword'], "sanitize_data");
    
    
    //8.确定数组的大小和唯一性
    //a.确定数组的大小count()
    //b.统计数组元素出现的频度array_count_values(array array)
    //c.确定唯一的数组元素array_unique()
    
    //9.数组排序
    
    //a.逆置数组元素顺序 array_reverse()
    //b.置换数组键和值 array_flip()
    //c.数组排序 sort()  各元素按值由低到高的顺序排列
    //d.保持键/值对的条件下对数组排序 asort()
    //e.以逆序对数组排序rsort()
    //f.保持键/值对的条件下以逆序对数组排序arsort()
    //g.数组自然排序 natsort()
    //h.不区分大小写的自然排序 natcasesort()
    //i.按键值对数组排序  ksort()
    //j.以逆序对数组键排序 krsort()
    //k.根据用户自定义规则排序 usort
    
    //10.合并、拆分、接合和分解数组
    
    //a.合并数组 array_merge()
    //b.递归追加数组 array_merge_recursive()
    //c.合并两个数组 array_combine
    //e.拆分数组 array_slice()
    //f.接合数组 array_splice()
    //g.求数组的交集array_intersect()
    //h.求关联数组的交集 array_intersect_assoc()
    //i.求数组的差集 array_diff()
    //j.求关联数组的差集 array_diff_assoc()
    
    //11.其他有用的数组函数
    
    //a.返回一组随机的键 array_rand()
    //b.随机洗牌数组元素 shuffle()
    //c.对数组中的值求和 array_sum()
    //e.划分数组 array_chunk()  input_array()
    

    相关文章

      网友评论

          本文标题:第五章 数组

          本文链接:https://www.haomeiwen.com/subject/ilrrdttx.html