一、作业
实例
<?php //数组 echo '1、创建空数组 <br>'; $arr = array ( ); print_r($arr); echo '<br>'; $arr1 = [ ]; var_dump($arr1); echo '<br>'; echo '2、创建索引数组 <br>'; $arr2 = [ '张三风', '王五', '张无忌' ]; echo $arr2[0]; echo '<br>'; $arr3 = array( '张三风', '王五', '张无忌' ); echo $arr3[2]; echo '<br>'; echo '3、创建关联数组 <br>'; $arr4 = [ 0 => '张三风', 1 => '王五', 2 => '张无忌' ]; var_dump( $arr4[0]); echo '<br>'; $arr5 = array( 'book' => '张三风', 'tv' => '王五', 'net' => '张无忌' ); print_r($arr5); echo '<br>'; echo '4、访问数据数据 <br>'; $arr6 = array( 'book' => '张三风', 'tv' => '王五', 'net' => '张无忌' ); print_r($arr6[tv]); echo '<br>'; echo '5、二维数组 <br>'; $arr7 = array( array( 'book' => '张三风', 'tv' => '王五', 'net' => '张无忌', ), array( 'book' => '张三', 'tv' => '李四', 'net' => '钱六', ) , array( 'book' => '李明', 'tv' => '郑七', 'net' => '田九', ) ); var_dump($arr7); echo '<hr>'; $arr7 = [ [ 'book' => '张三风', 'tv' => '王五', 'net' => '张无忌', ], [ 'book' => '张三', 'tv' => '李四', 'net' => '钱六', ] , [ 'book' => '李明', 'tv' => '郑七', 'net' => '田九', ] ]; print_r($arr7); echo '<hr>'; echo $arr7[0][book]; echo '<hr>'; echo '6、三维数组 <br>'; $arr8 = [ [ 'book' => '张三风', 'tv' => '王五', 'net' => '张无忌', 'arr' => [ 0, 1, 2, ], ], [ 'book' => '张三', 'tv' => '李四', 'net' => '钱六', 'arr' => [ 4, 5, 6, ], ] , [ 'book' => '李明', 'tv' => '郑七', 'net' => '田九', 'arr' => [ 7, 8, 9, ], ] ]; print_r($arr8); echo '<hr>'; echo '7、二维数组循环 <br>'; $arr9 = [ [ 'book' => '张三风', 'tv' => '王五', 'net' => '张无忌', ], [ 'book' => '张三', 'tv' => '李四', 'net' => '钱六', ] , [ 'book' => '李明', 'tv' => '郑七', 'net' => '田九', ] ]; foreach ($arr9 as $k => $v){ print_r($v); echo '<br>'; } echo '<hr>'; foreach ($arr9 as $v){ print_r($v); echo '<br>'; } echo '<hr>'; echo '8、三维数组循环 <br>'; $arr10 = [ [ 'book' => '张三风', 'tv' => '王五', 'net' => '张无忌', 'arr' => [ 0, 1, 2, ], ], [ 'book' => '张三', 'tv' => '李四', 'net' => '钱六', 'arr' => [ 4, 5, 6, ], ] , [ 'book' => '李明', 'tv' => '郑七', 'net' => '田九', 'arr' => [ 7, 8, 9, ], ] ]; foreach ($arr10 as $v){ foreach ($v as $vv){ print_r($vv); } echo '<hr>'; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
二、手写
三、总结
学习了php数据,基本掌握了一维、二维、三维数据的用法。