1、创建数组
<?php //空数组 $arr = [];
2、关联数组、索引数组
<?php //索引数组 $arr = ['小明','小红','小强']; //关联数组 $arr1 = ['xiaoming' => '小明','xiaohong' => '小红','xiaoqiang' => '小强];
3、访问数组的数据
<?php $arr = ['小明','小红','小强']; echo $arr[0]; $arr1 = ['xiaoming' => '小明','xiaohong' => '小红','xiaoqiang' => '小强]; print_r($arr1['xiaoming']);
4、二维数组
<?php //二维数组 $arr = [ [ 'name' => '小明', 'sex' => '男' ], [ 'name' => '小红', 'sex' => '女' ], [ 'name' => '小强', 'sex' => '男' ] ]; //三维数组 $arr = [ [ 'name' => '小明', 'sex' => '男', 'skill' => [ '跆拳道', '游泳', '绘画' ] ], [ 'name' => '小红', 'sex' => '女', 'skill' => [ '唱歌', '跳舞', '体操' ] ], [ 'name' => '小强', 'sex' => '男', 'skill' => [ '武术', '田径', '编程' ] ] ];
5、二维数组循环、三维数组循环
<?php //二维数组循环 $arr = [ [ 'name' => '小明', 'sex' => '男' ], [ 'name' => '小红', 'sex' => '女' ], [ 'name' => '小强', 'sex' => '男' ] ]; foreach($arr as $v){ echo '姓名:'.$v['name'].'.'.'性别:'.$v['sex']; echo '<br>'; }; //三维数组循环 $arr1 = [ [ 'name' => '小明', 'sex' => '男', 'skill' => [ '跆拳道', '游泳', '绘画' ] ], [ 'name' => '小红', 'sex' => '女', 'skill' => [ '唱歌', '跳舞', '体操' ] ], [ 'name' => '小强', 'sex' => '男', 'skill' => [ '武术', '田径', '编程' ] ] ]; foreach($arr1 as $k => $v){ echo '姓名:'.$v['name'].'...'; echo '性别:'.$v['sex'].'...'; echo '技能:'; foreach($v['skill'] as $kk => $vv){ echo $vv.'.'; }; echo '<br>'; };
以上代码全部是在浏览器中手打,手打代码的时候会经常忘记敲,和;,估计写熟练了以后会避免此类低级错误,稍后会验证一下有无错误,有的话会马上改过来。关于三维数组的循环还是有一点懵,应该多写几遍就会掌握了。后面附上今天晚上的课堂笔记。