Blogger Information
Blog 32
fans 2
comment 0
visits 27939
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组的定义,访问,遍历
简行
Original
797 people have browsed it

一.数组的定义与访问

1.索引数组: 位置敏感,访问严重依赖元素在数组中的位置;键是数值的索引,表示元素的位置;默认从0开始递增

  1. //索引数组
  2. //定义:
  3. $arr_day=['春节',"夏季","秋季","冬季"];//等同与 $arr_day=[0=>'春节',1=>"夏季",2=>"秋季",3=>"冬季"]
  4. //按索引访问:
  5. echo $arr_day[1]."===".$arr_day[3];// 输出 夏季===冬季

2.关联数组:键的类型是字符串,访问与元素在数组中的位置无关,只与它的键名相关

  1. //关联数组
  2. //定义:
  3. $calss = ["id"=>1,'name'=>"王明",'sex'=>'男',"cla"=>"一班"];
  4. //按键名访问
  5. echo "姓名:".$calss['name']."==>".$calss['cla']; //输出 姓名:王明==>一班

二. 数组的遍历

1.指针遍历

  1. //指针遍历
  2. $users =["id"=>'1021','name'=>"陈松江",'sex'=>'男',"money"=>666.66,"pwd"=>"888888"];
  3. // current():获取指针当前位置的数组元素的值value
  4. // key():获取指针当前位置的数组元素的键key
  5. echo "key=>".key($users)."-------value=>".current($users),"<br>";
  6. //next():指针向后位移一位
  7. next($users);
  8. echo "next()后指定后移:key=>".key($users)."-------value=>".current($users),"<br>";
  9. next($users);
  10. echo "key=>".key($users)."-------value=>".current($users),"<br>";
  11. // prev():指针前移
  12. prev($users);
  13. echo "prev()指针前移:key=>".key($users)."-------value=>".current($users),"<br>";
  14. // end()直接到最后一个元素的位置
  15. end($users);
  16. echo "end()指针直接到最后一个元素:key=>".key($users)."-------value=>".current($users),"<br>";\
  17. // reset():指针复位(回到第一个)
  18. reset($users);
  19. echo "reset()指针复位回到第一个:key=>".key($users)."-------value=>".current($users),"<br>";

2.while循环遍历

  1. //while循环遍历
  2. $goods =["id"=>'615','name'=>"龙虾",'unit'=>'只',"money"=>400,"num"=>"1"];
  3. while(true){
  4. echo "key=>".key($goods)."-------value=>".current($goods),"<br>";
  5. if(next($goods)){
  6. }else{
  7. break;
  8. }
  9. }

3.for循环遍历

  1. // for循环遍历
  2. $good =["id"=>'66','name'=>"哈密瓜",'unit'=>'斤',"money"=>60,"num"=>"4"];
  3. for($i=0;$i<count($good);$i++){
  4. echo "key=>".key($good)."-------value=>".current($good),"<br>";
  5. next($good);
  6. }

4.foreach循环遍历

  1. //foreach循环遍历 :不需要指针复位,需要手工指针指针, 自动后移
  2. //创建一个二维数组
  3. $shop_arr=[];
  4. $shop_arr[]=['id'=>101,"shopname"=>"闽南一号","phone"=>15635673557];
  5. $shop_arr[]=['id'=>111,"shopname"=>"闽南二号","phone"=>14642365236];
  6. $shop_arr[]=['id'=>121,"shopname"=>"闽南三号","phone"=>18850471668];
  7. foreach($shop_arr as $shop){
  8. printf("序号=%d,店面=%s,电话=%d",$shop['id'],$shop['shopname'],$shop['phone']);
  9. echo '<br>';
  10. }

5.list遍历

  1. // list()解构/解析: 索引数组
  2. list($id, $name,$phone) = [111,"admin","18850146677"];
  3. echo $id, $name, $phone;
  4. echo '<br>';
  5. // list()解构/解析: 关联数组
  6. //创建一个二维数组
  7. $user_arr=[];
  8. $user_arr[]=['id'=>333,"username"=>"闽南一号","ph"=>15635673557];
  9. $user_arr[]=['id'=>444,"username"=>"闽南二号","ph"=>14642365236];
  10. $user_arr[]=['id'=>555,"username"=>"闽南三号","ph"=>18850471668];
  11. foreach ($user_arr as list('id'=>$id, 'username'=>$username, 'ph'=>$ph) ) {
  12. printf('id=%s, 姓名=%s, 电话: %s<br>', $id, $username, $ph);
  13. }

总结

foreach循环遍历具有其他几种循环遍历特性,推荐优先使用,特别是遍历多维数组

Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:数组是php最最最最最重要的,所以一定要吃透!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post