一个简单的索引数组
这是一个关联数组
关联数组里要存在键值。
实例
<?php // 这是一个关联数组 $arr = [ 'nanxie' => 'man', 'nvxie' => 'wpmen', 'tongxie' => 'boy' ]; print_r($arr);//这是打印数组 echo $arr['nanxie'];//这是输出数组 ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
这是一个二维数组
实例
<?php $arr = [ [ 'nanxie' => 'man', 'size' => 40 ], [ 'nvxie' => 'women', 'size' => 30 ], [ 'tongxie' => 'boy', 'size' => 10 ] ]; print_r($arr); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
这是一个三维维数组
实例
<?php $arr = [ [ 'nanxie' => 'man', 'size' => '40', 'color' => [ '黑色', '白色' ] ], [ 'nvxie' => 'women', 'size' => '30', 'color' => [ '红色', '橙色' ] ], [ 'tongxie' => 'boy', 'size' => '10', 'color' => [ '蓝色', '紫色' ] ] ]; echo $arr[0]['nanxie'].' --- ';//访问三维数组的方式,其中 .' --- ' 为连接符 ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
循环数组
实例
<?php $arr = [ 'man', 'women', 'boy' ]; foreach( $arr as $v ){ echo $v; echo '<hr>'; } ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
循环二维数组
实例
<?php $arr = [ [ 'nanxie' => 'man', 'size' => 40 ], [ 'nvxie' => 'women', 'size' => 30 ], [ 'tongxie' => 'boy', 'size' => 10 ] ]; foreach( $arr as $k=>$v ){ foreach ($v as $key => $value) { echo $value; echo '<hr>'; } } ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
循环三维数组
实例
<?php $arr = [ [ 'pinlei' => 'man', 'size' => '40', 'color' => [ '黑色', '白色' ] ], [ 'pinlei' => 'women', 'size' => '30', 'color' => [ '红色', '橙色' ] ], [ 'pinlei' => 'boy', 'size' => '10', 'color' => [ '蓝色', '紫色' ] ] ]; foreach( $arr as $v ){ echo $v['pinlei'].' <br> '.$v['size'].'<br>'; foreach($v['color'] as $bb){ echo $bb.'<br>'; } echo '<hr>'; } ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例