听说code能改变world,所以就学了

2014.01.20数组遍历(1)

<?php
   /*php数组的多种声明
     
   */
     header("content-type:text/html; charset=utf-8");
     //1.索引数组
     $user[0]=1;
     $user[1]="zhangshan";
     $user[2]="sex";
     $user[3]=20;
     echo"<pre>";
     print_r($user);
     //关联数组
     $class["name"]="lee";
     $class["age"]=20;
     $class["sex"]="nan";
     echo"<pre>";
     echo$class;
     //使用array()函数声明
     $user=array(1,"lee","nan");
     //这种声明方式是默认为索引数组/
     echo"<pre>";
     print_r($user);
     
     //要想指定下标则需要使用键值符号=>
     $user=array("id"=>1,1=>"lee","sex"=>"nan");
     echo"<pre>";
     print_r ($user);
     //多维数组
     $user=array(
        
        array(1,"zhansan","nan"),
        //为第0个数组
        array(2,"lisi","nan"),
        //为第1个数组
        array(3,"wwu","nan"),
        //为第2个数组
        );
     echo $user[1][1];
     //或者直接给里面的数组给下标
      $user=array(
        
        "one"=>array(1,"zhansan","nan"),
        //下标为one的数组
        "two"=>array(2,"lisi","nan"),
        //下标为two的数组
        "three"=>array(3,"wwu","nan"),
        //three
        );
     echo $user["two"][1];
     //数组遍历
     //for循环遍历
     $user=array("id","lee","nan");
     for($i=0;$i<count($user);$i++){
           echo $user[$i]."<br>";
     }
     foreach($user as $key=> $info){
                echo $key."+".$info;
     }
     
     ?>
     

 

posted @ 2014-01-20 23:18  李腾  阅读(152)  评论(0编辑  收藏  举报

如果我加了黑链呢:http://liteng.org