Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:完成的不错, 案例中的内容你可以自定义的
1.1 创建数组
//老版本写法 $arr = array(); var_dump( $arr ); //新版本写法 $arrs = []; var_dump( $arrs );
1.2 索引数组
$arr = array( '欧阳克', '黄蓉', '郭靖' ); var_dump( $arr ); $arrs = [ '欧阳克', '黄蓉', '郭靖' ]; var_dump( $arrs );
1.3 创建关联数组
$arr = array( 'ouyangke' => '欧阳克', 'huangrong' => '黄蓉', 'guojing' => '郭靖' ); var_dump( $arr ); $arrs = [ 'ouyangke' => '欧阳克', 'huangrong' => '黄蓉', 'guojing' => '郭靖' ]; var_dump( $arrs );
1.4 输出数组值
$arr = [ '欧阳克', '黄蓉', '郭靖' ]; echo $arr[0]; echo '<hr>'; echo $arr[1]; echo '<hr>'; echo $arr[2]; echo '<hr>'; $arrs = [ 'ouyangke' => '欧阳克', 'huangrong' => '黄蓉', 'guojing' => '郭靖' ]; echo $arrs['ouyangke']; echo '<hr>'; echo $arrs['huangrong']; echo '<hr>'; echo $arrs['guojing']; echo '<hr>';
1.5 打印数组
$arr = [ '欧阳克', '黄蓉', '郭靖' ]; print_r($arr);
二、多维数组
2.1 二维数组
$arr = array( array( 'name' => '欧阳克', 'age' => 18, ), array( 'name' => '黄蓉', 'age' => 16, ), array( 'name' => '郭靖', 'age' => 22, ) ) var_dump($arr); print_r($arr);
2.2 三维数组
$arr = [ [ 'name' => '欧阳克', 'age' => 18, 'gongfu' => [ '神驼雪山拳', '灵蛇拳法' ] ], [ 'name' => '黄蓉', 'age' => 16,, 'gongfu' => [ '碧波掌法', '落英神剑掌' ] ], [ 'name' => '郭靖', 'age' => 22,, 'gongfu' => [ '降龙十八掌', '九阴真经' ] ] ]; var_dump($arr); print_r($arr);
2.3 多维数组访问
$arr = [ [ 'name' => '欧阳克', 'age' => 18, 'gongfu' => [ '神驼雪山拳', '灵蛇拳法' ] ], [ 'name' => '黄蓉', 'age' => 16,, 'gongfu' => [ '碧波掌法', '落英神剑掌' ] ] ] echo $arr[0]['name'].' --- '; echo $arr[0]['gongfu'][0].' --- '; echo $arr[0]['gongfu'][1];
三、php 数组循环
数组循环,是对二维数组以上的数组进行循环
1、foreach
$arr = array( 'ouyangke' => '欧阳克', 'huangrong' => '黄蓉', 'guojing' => '郭靖' ); foreach( $arr as $v ){ echo $v; echo '<hr>'; }
2、key 和 value
$arr = array[ 'ouyangke' => '欧阳克', 'huangrong' => '黄蓉', 'guojing' => '郭靖' ]; foreach( $arr as $k=>$v ){ echo $k . ' --- ' . $v; echo '<hr>'; }
3.循环多维数组
$arr = [ [ 'name' => '欧阳克', 'age' => 18 ], [ 'name' => '黄蓉', 'age' => 16 ], [ 'name' => '郭靖', 'age' => 22 ] ]; foreach( $arr as $k=>$v ){ print_r($v); echo '<hr>'; }
$arr = [ [ 'name' => '欧阳克', 'age' => 18 ], [ 'name' => '黄蓉', 'age' => 16 ], [ 'name' => '郭靖', 'age' => 22 ] ]; foreach( $arr as $k=>$v ){ foreach ($v as $key => $value) { echo $value; echo '<hr>'; } }
$arr = [ [ 'name' => '欧阳克', 'age' => 18 ], [ 'name' => '黄蓉', 'age' => 16 ], [ 'name' => '郭靖', 'age' => 22 ] ]; foreach( $arr as $k=>$v ){ foreach ($v as $key => $value) { echo $value; echo '<hr>'; } }
$arr = [ [ 'name' => '欧阳克', 'age' => 18 ], [ 'name' => '黄蓉', 'age' => 16 ], [ 'name' => '郭靖', 'age' => 22 ] ]; foreach( $arr as $k=>$v ){ echo $v['name'].' --- '.$v['age']; echo '<hr>'; }