- <?php
- /*for循环遍历*/
- $arr = array( array("张三","20","男"),array("李四","25","男"),array("王五","19","女") );
- for($i=0;$i<3;$i++)
- {
- for($j=0;$j<3;$j++)
- {
- echo $arr[$i][$j]." ";
- }
- echo "<br/>";
- }
-
- /*foreach循环遍历*/
- $arr = array('1'=>array('name'=>'张三','year'=>'20','sex'=>'男'),
- '2'=>array('name'=>'李四','year'=>'25','sex'=>'男'),
- '3'=>array('name'=>'王五','year'=>'19','sex'=>'女')
- );
- foreach($arr as $key => $link)
- {
- echo $link['name'].$link['year'].$link['sex']."<br/>";
- }
-
- /*foreach*/
- $arr = array( '1'=>array('name'=>'张三','year'=>'12','sex'=>'男'),
- '2'=>array('name'=>'李四','year'=>'12','sex'=>'男'),
- '3'=>array('name'=>'王五','year'=>'13','sex'=>'女')
- );
- foreach($arr as $key => $link)
- {
- foreach($link as $key1 => $val)
- {
- echo $val." ";
- }
- echo "<br/>";
- }
-
-
- ?>
- <?php
- $arr = array('0'=>'a','1'=>'b','2'=>'c');
- for($i=0;$i<count($arr);$i++)
- {
- echo $arr[$i]."<br/>";
- }
-
- ?>
- <?php
- /*list()和each()遍历数组*/
- $arr = array('0'=>'张三','1'=>'李四','2'=>'王五');
- //使用list()函数获取each()函数中返回数组的值,并分别赋给$num,$name两个变量,然后用while输出
- while(list($num,$name)=each($arr))
- {
- echo $num.' '.$name."<br/>";
- }
-
- ?>
|