在PHP中数组有两种形式,索引数组和关联数组 索引数组:用整数做下标,默认从0开始,后面依次加一 关联数组:用字符串做下标,通过=>符号将下标和值关联起来 - <?php
- //1、索引数组的声明
- $stu=array('tom','berry','ketty'); //索引数组
- print_r($stu); //输出数组 Array ( [0] => tom [1] => berry [2] => ketty )
- echo '<hr>';
- echo $stu[0],'<br>'; //tom
- echo $stu[1],'<br>'; //berry
- echo $stu[2],'<hr>'; //ketty
- ------------------------------------------
- //2、关联数组
- $emp=array('name'=>'李白','sex'=>'男','age'=>22);
- print_r($emp); //Array ( [name] => 李白 [sex] => 男 [age] => 22 )
- echo '<hr>';
- echo $emp['name'],'<br>'; //李白
- echo $emp['sex'],'<br>'; //男
- echo $emp['age']; //22
复制代码练习:写出数组的下标
- $array=array(1=>'a','b','c','d');
- print_r($array); //Array ( [1] => a [2] => b [3] => c [4] => d )
- echo '<br>';
- --------------------------
- $array=array('a',2=>'b','c',5=>'d');
- print_r($array); //Array ( [0] => a [2] => b [3] => c [5] => d )
- echo '<br>';
- ----------------------------
- $array=array('a','name'=>'b','c','sex'=>'d');
- print_r($array); //Array ( [0] => a [name] => b [1] => c [sex] => d )
- echo '<br>';
- ------------------------------
- $array=array(1=>'a',1=>'b',1=>'c','d');
- print_r($array); //Array ( [1] => c [2] => d )
复制代码
|