Blogger Information
Blog 9
fans 1
comment 0
visits 8796
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
关联数组与索引数组的书写与输出
江水的博客
Original
1040 people have browsed it

实例

<?php
//关联数组
$arr1=array(
    'name'=>'张三',
    'age'=>25,
    'sex'=>'男',
    'address'=>'山东'
);
   

//索引数组
$arr2=[5,8,9,20,3];
//循环输出数组
foreach($arr1 as $v){
    echo $v."<br>";
}
//二维数组
$arr3=[
    ['西游记','吴承恩'],
    ['红楼梦','曹雪芹'],
    ['三国演义','罗贯中'],
    ['水浒传','施耐庵']
];
echo "<hr>";
//循环输出二维数组
foreach($arr3 as $v){
     foreach ($v as $vv) {
         echo $vv;
     }
     echo "<br>";
}
echo '<hr>';
//三维数组
$arr4=[
    [
        'name'=>'四大名著',
        'count'=>['西游记','红楼梦','三国演义','水浒传']
    ],
    [
        'name'=>'四大发明',
       'count'=> ['指南针','火***','造纸术','印刷术']
    ],
];
//循环输出三维数组
foreach($arr4 as $value){
  echo $value['name'].'<br>';
  foreach($value['count'] as $v){
      echo $v.'<br>';
  }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

最后运行效果图:

array.png

Correction status:qualified

Teacher's comments:关联数组与索引数组的区别主要在于键名的类型上,如果是字符串则关联, 元素与位置无关,可以按键名访问, 索引数组的键名类型是整数,与元素位置相关, 按索引访问
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments