• PHP数组和数据结构(上)


    1.数组的声明

    两种方法:

    • 直接给数组元素赋值
    • 使用array()函数声明

    说明:

    (1)索引数组的下标可以是非连续的值,只要在初始化时指定非连续的下标值即可

    • 如果指定的下标值已经声明过,则属于对变量重新赋值。
    • 如果没有指定索引值的元素与指定索引值的元素混在一起赋值时,没有指定索引值的元素默认索引值,将紧跟指定索引值元素中最高的索引值递增。

    代码示例:

    <?php
    $a[]=1;
    $a[10]="高某";
    $a[]="A公司";   // 此时A公司的下标成为11
    $a[]="北京市";   //北京市的下标为12
    $a[]="(010)987654321"; //下标是13
    $a[10]="gao@qq.com";//下标是11,下标11的值改为gao@qq.com
    $a[]="IT"; //IT的下标值为14
    var_dump($a);
    ?>

    运行结果:

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

    使用array()语言构造来新建一个数组,它接受一定数量用逗号分隔的key => value参数对

     代码示例:

    <?php
    //如果不使用 => 符号指定下标,默认为索引数组。默认的索引值也是从0开始一次增加
    $a=array(1,"高某","A公司","北京市","(010)987654321","gao@qq.com");
    //使用 => 可以指定非连续的索引值
    $a1=array(1,10=>"高某","A公司","北京市",10=>"(010)987654321","gao@qq.com");
    var_dump($a);
    echo "<br/>";
    var_dump($a1);
    ?>

    运行结果:

     2.数组元素的访问

    代码示例:

    <?php
    $a=array(1,"高某","A公司","北京市","(010)987654321","gao@qq.com");
    //使用 => 可以指定非连续的索引值
    $a1=array('xiaoming'=> "IT",'xiaohei'=>"english",'xiaolan'=>"chinese");
    //直接指定元素下标
    echo $a[1];
    echo "<br/>";
    //指定键:
    echo $a1['xiaohei'];
    ?>

    运行结果:

    3.二维数组

    (1)数字索引方式

    $a=array(array(1,"高某","A公司","北京市","(010)987654321","gao@qq.com"),
            array(2,"李某","B公司","上海市","(010)987654323","li@qq.com"),
            array(3,"杨某","C公司","济南市","(010)987654324","yang@qq.com"),
            array(4,"孙某","D公司","威海市","(010)987654325","sun@qq.com")
    );

    (2)使用array()函数

    $a2=array("北京联系人"=>array(1,"高某","A公司","北京市","(010)987654321","gao@qq.com"),
              "上海联系人"=>array(2,"李某","B公司","上海市","(010)987654323","li@qq.com"),
              "济南联系人"=>array(3,"杨某","C公司","济南市","(010)987654324","yang@qq.com")
    );

    (3)二维数组的访问

    <?php
    $a=array(array(1,"高某","A公司","北京市","(010)987654321","gao@qq.com"),
            array(2,"李某","B公司","上海市","(010)987654323","li@qq.com"),
            array(3,"杨某","C公司","济南市","(010)987654324","yang@qq.com"),
            array(4,"孙某","D公司","威海市","(010)987654325","sun@qq.com")
    );
    echo $a[1][1];
    echo "<br/>";
    $a2=array("北京联系人"=>array(1,"高某","A公司","北京市","(010)987654321","gao@qq.com"),
              "上海联系人"=>array(2,"李某","B公司","上海市","(010)987654323","li@qq.com"),
              "济南联系人"=>array(3,"杨某","C公司","济南市","(010)987654324","yang@qq.com")
    );
    echo $a2["上海联系人"][1];
    ?>

    4 .数组的遍历

    (1)for循环遍历

    <?php
    $a=array(array(1,"高某","A公司","北京市","(010)987654321","gao@qq.com"),
            array(2,"李某","B公司","上海市","(010)987654323","li@qq.com"),
            array(3,"杨某","C公司","济南市","(010)987654324","yang@qq.com"),
            array(4,"孙某","D公司","威海市","(010)987654325","sun@qq.com")
    );
    //count是得到一维长度
    echo count($a)."<br/>";
    for($i=0;$i<count($a);$i++){
        for ($j=0;$j<count($a[$i]);$j++){
            echo $a[$i][$j];
        }
        echo "<br/>";
    }
    ?>

    运行结果:

     (2)foreach遍历

     

    <?php
    $a[]=1;
    $a[10]="高某";
    $a[]="A公司";   // 此时A公司的下标成为11
    $a[]="北京市";   //北京市的下标为12
    $a[]="(010)987654321"; //下标是13
    $a[10]="gao@qq.com";//下标是11,下标11的值改为gao@qq.com
    $a[]="IT"; //IT的下标值为14
    foreach ($a as $value){
        echo $value."<br>";
    }
    ?>

    运行结果:

     

    <?php
    $a=array(
            "ID"=>"1",
            "name"=>"xiaohua",
            "city"=>"shanghai",
            "job"=>"IT"
            
    );
    foreach ($a as $key=>$value){
        echo "<dd>$key:$value<dd>";
    }
    ?>

    运行结果:

  • 相关阅读:
    清除浮动的集中方式
    css动画
    [Asp.net]c#中的斜杠和反斜杠
    C# ASPX页面做上传提示:超过了最大请求长度,解决方法
    在asp.net中用客户端上传控件上传文件( 需要注意的)
    jquery clone 与 clone(true) 的区别
    【转载】用div+css模拟表格对角线
    .net2.0中的Json序列化数据
    jquery操作checkbox的选中与不选中,解决只有一次有用。
    转:不同服务器数据库之间的数据操作[含远程数据库备份]
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/9243169.html
Copyright © 2020-2023  润新知