1.数组
header("content-Type: text,html;charset=utf-8"); $arr = array(); echo $arr; print_r($arr);
2.索引数组
$arr = [ '小乔', '貂蝉', '王昭君' ]; echo '<per>'; echo $arr[0]; echo '<hr>'; echo $arr[1]; echo '<hr>'; echo $arr[2]; echo '<hr>';
3.关联数组
$arrs = [ 'xiaoqiao' => '小乔', 'diaochan' => '貂蝉', 'wangzhaojun' => '王昭君' ]; echo '<per>'; echo $arrs['xiaoqiao']; echo '<hr>'; echo $arrs['diaochan']; echo '<hr>'; echo $arrs['wangzhaojun']; echo '<hr>';
4.二维数组
$arr = array( 'xiaoqiao' => array( 'name' => '小乔', 'age' => 14, ), 'diaochan' => array( 'name' => '貂蝉', 'age' => 16, ), 'wangzhaojun' => array( 'name' => '王昭君', 'age' => 18, ), ); echo '<pre>'; print_r($arr);
5.三维数组
$arr = array( 'xiaoqiao' => array( 'name' => '小乔', 'age' => 14, 'gongfu' =>['花会枯萎','永不凋零'] ), 'diaochan' => array( 'name' => '貂蝉', 'age' => 16, 'gongfu' =>['花会枯萎','永不凋零'] ), 'wangzhaojun' => array( 'name' => '王昭君', 'age' => 18, 'gongfu' =>['花会枯萎','永不凋零'] ), ); echo '<pre>'; print_r($arr);
6.循环数组
$arr =[ 'ouyangke' => '欧阳克', 'huangrong' => '黄蓉', 'guojing' => '郭靖' ]; $num = 1; foreach($arr as $a){ $num = $num + 1; echo $num; }
7.循环二维数组
$arr = [[ 'name' => '欧阳克', 'age' => 18 ], ['name' => '黄蓉', 'age' => 16], ['name' => '郭靖', 'age' => 16], ]; foreach ($arr as $k => $v) { echo $v['name'] . '-' . $v['age']; echo '<hr>'; }
8.循环三维数组
$arr = [ [数组中 'name' => '欧阳克', 'age' => 18 ], ['name' => '黄蓉', 'age' => 16], ['name' => '郭靖', 'age' => 16], ]; foreach ($arr as $k => $v) { foreach($v as $key => $value){ echo $value; echo '<hr>'; } }
数组中,每个数据都有两个,一个是key(下标),一个是value(值),键值对。
数组循环,是对二维数组以上的数组进行循环。
要有一个数组,把数组放到括号里,第二个参数as,把第一个参数的值给第三个参数。