zoukankan      html  css  js  c++  java
  • PHP学习随笔(3):数组

    三种数组类型:

      1.数值数组:带有数字id键的数组

      2.关联数组:数组中的每个id键关联一个值

      3.多维数组:包含一个或多个数组的数组


    数值数组

      数值数组存储的每个元素有带有一个数字id键(索引?)

      创建数值数组的两种方法:

    $names = array("Peter","Quagmire","Joe");
    $names[0] = "Peter";
    $names[1] = "Quagmire";
    $names[2] = "Joe";

      以上代码都可以创建数值数组,下面是在脚本中使用这些id

    <?php
    
    $names[0] = "Peter";
    $names[1] = "Quagmire";
    $names[2] = "Joe";
    
    echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors";
    ?>

      输出结果:

    Quagmire and Joe are Peter's neighbors

    关联数组

      通过关联数组,我们可以把值作为键,并向它们赋值(类似于C#中的字典集合)

      下面为关联数组的创建方法

    $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
    $ages['Peter'] = "32";
    $ages['Quagmire'] = "30";
    $ages['Joe'] = "34";

      在脚本中使用

    <?php
    
    $ages['Peter'] = "32";
    $ages['Quagmire'] = "30";
    $ages['Joe'] = "34";
    
    echo "Peter is " . $ages['Peter'] . " years old.";
    ?>

      输出结果

    Peter is 32 years old.

    多维数组

      在多维数组中,每个元素的值都是一个数组,以次类推

    $families = array
    (
      "Griffin"=>array
      (
      "Peter",
      "Lois",
      "Megan"
      ),
      "Quagmire"=>array
      (
      "Glenn"
      ),
      "Brown"=>array
      (
      "Cleveland",
      "Loretta",
      "Junior"
      )
    );

      输出一个值

    echo "Is " . $families['Griffin'][2] . 
    " a part of the Griffin family?"; 

      输出结果

    Is Megan a part of the Griffin family?
  • 相关阅读:
    C++函数模板的显示调用与隐式调用
    git显示颜色配置
    STL容器元素应满足的条件
    vector缩减容量
    PAT (Basic Level) Practise:1036. 跟奥巴马一起编程
    Core Java Volume I — 4.10. Class Design Hints
    Core Java Volume I — 4.7. Packages
    蓝牙(Profile)构成
    Android开发之Java必备基础
    主机控制器接口(HCI)
  • 原文地址:https://www.cnblogs.com/kire/p/3056804.html
Copyright © 2011-2022 走看看