php之数组

数组分类:

  1.索引数组. 索引值从0开始,依次递增。

  2.使用array()函数声明数组

<?php
    // 1.直接为数组元素赋值即可声明数组
    $contact_index[0] = 1;
    $contact_index[1] = "高某";
    $contact_index[2] = "A公司";
    $contact_index[3] = "北京市";
    $contact_index[] = "gao@a.com";
    var_dump($contact_index);
    $contact_key["ID"] = "2";
    $contact_key["姓名"] = "峰某";
    $contact_key["公司"] = "B公司";
    $contact_key["邮箱"] = "feng@b.com";
    var_dump($contact_key);
    // 2.使用array()函数声明数组
    $contact_key_array = array(
        "ID" => 1;
        "姓名" => "峰某";
        "公司" => "B公司";
        "邮箱" => "feng@b.com";
    );
    var_dump($contact_key_array);
?>

 数组的遍历:

  1.for语句遍历数组

  2.foreach语句遍历数组

<?php
    //用for语句遍历数组
    $cars=array("Volvo","BMW","SAAB");
    $arrlength=count($cars);
    for($i=0;$i<$arrlength;$i++) {
    echo $cars[$i];
    echo "<br>";
    }   
?>
<?php
//foreach语句遍历关联数组 $age=array("Bill"=>"35","steve"=>"37","peter"=>"43"); foreach($age as $x=>$x_value) { echo "Key=".$x.", Value=".$x_value; echo "<br>"; } ?>

 预定义数组

  1.它就是一个特殊数组,操作方式没有区别。

  2.不用声明它们,每个php脚本中默认存在。

  3.它们在全局范围内自动生效。

      预定义数组     说明

      $_SERVER    变量由web服务器设定或者直接与当前脚本的执行环境相关联

   $_ENV         执行环境提交至脚本的变量

  $_GET   经由URL请求提交至脚本的变量

  $_POST   经由HTTP POST 方法提交至标本的变量

  $_REQUEST 经由GET,POST,COOKIE机制提交至脚本的变量

  $_FILES   经由HTTP POST 文件上传而提交至脚本的变量

  $_COOKIE  经由HTTP Cookie 方法提交至脚本的变量

  $_SESSION   当前注册给脚本会话的变量

  $_GLOBALS   包含一个引用指向每个当前脚本的全局范围内的有效的变量,该数组的键名 为全局变量的名称。

合并数组

array_merge()函数将数组合并到一起,返回一个联合的数组。

array array_merge(array array1 array2.....arrayN)

<?php
    $fruits=array("apaple","banana","pear");
    $numbered=array("1","2","3");
    $cards=array_merge($fruits,$numbered);
    print_r($cards); 
// output
//Array ( [0] => apaple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 )
?>

 拆分数组

    array_slice()函数将返回数组中的一部分,从键offset开始,到offset+length位置结束

 array array_slice(array array,int offset,[int length])

 offset 为正值时从前面开始,为负数时从后面开始

<?php
    $fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon","Watermelon");
    $subset=array_slice($fruits,3);
    print_r($subset);
//output
//Array ( [0] => Pear [1] => Grape [2] => Lemon [3] => Watermelon ) ?>

<?php
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon","Watermelon");
$subset=array_slice($fruits,2,-2);
print_r($subset);
//output
//Array ( [0] => Orange [1] => Pear [2] => Grape )
?>

数组头添加元素:

array_unshift函数在数组头添加元素

<?PHP
   $fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon");
   array_unshift($fruits,"AAAA","BBBB");
   print_r($fruits);
//output
//Array ( [0] => AAAA [1] => BBBB [2] => Apple [3] => Banana [4] => Orange [5] => Pear [6] => Grape [7] => Lemon ) 

?>

数组头删除元素:

array_shift删除数组头元素

<?php
   $fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon");
   array_shift($fruits);
   print_r($fruits);
//output
//Array ( [0] => Banana [1] => Orange [2] => Pear [3] => Grape [4] => Lemon ) 
?>

数组尾部添加元素:

array_push函数在数组尾部添加元素

<?php
    $fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon");
    array_push($fruits,"AAAA","BBBB");
    print_r($fruits);
//output
//Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Pear [4] => Grape [5] => Lemon [6] => AAAA [7] => BBBB ) 

?>

 数组尾部删除元素:

array_pop函数删除数组尾部元素,返回的是数组的最后一个元素。

<?php
   $fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon");
   $result=array_pop($fruits);
   print_r($result);
   echo "<br>";
   print_r($fruits);
//output
//$result  返回Lemon
//$fruits  返回Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Pear [4] => Grape ) 
?>

posted on 2016-08-26 15:58  Time_dog  阅读(222)  评论(0编辑  收藏  举报

导航