php 数组的定义和初始化学习笔记

什么是数组?数组是一种编程结构,它是一个存储一组或一系列数值的变量,比如人口普查时对个人的身份登记,如姓名、性别、民族、出生等就可作为数组.

php中创建数组使用array()结构来定义,比如:

$people=array('name','sex','nation','brith');

而如何显示数组中的各元素的值,我们则是使用从0开始的索引,索引号在变量名称后的方括号中,比如:

  1. <?php 
  2. $people=array('name','sex','nation','birth'); 
  3. echo $people[2]; 
  4. ?> 

输出的$people[2]就是显示的是nation(索引第一项从0计数),php除了支持数字索引数组以外,还支持相关数组,所谓相关数组,就是可自定义关键字来替换不直观的数字索引,比如:

  1. <?php 
  2. $peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth'); 
  3. echo $peoples['cs']; 
  4. ?> 

使用相关数组使得输出的选择很直观(不需要预先计算索引号然后输出),定义的关键字和值之间使用“=>”符号定义.

根据php数组元素的两种显示方式,还可以如变量一样无需array()声明和初始化,直接自动创建数字,比如:

  1. $people[0]='name'
  2. $people[1]='sex'
  3. $people[2]='nation'
  4. $people[3]='brith';   
  5. //或者 
  6. $peoples['xm']='name'
  7. $peoples['xb']='sex'
  8. $peoples['mz']='nation'
  9. $peoples['cs']='birth';//该数组的大小根据所增加元素的多少动态的变化。 

数组元素的显示:

在如上使用的无论$people[2]也好,无论$peoples['cs']也好,都只是输出已知的明确位置的数组元素值,如何快速输出所有或部分的数组元素,使用循环语句无疑是最快的方法:

  1. <?php 
  2. $people=array('name','sex','nation','birth'); 
  3. for ($i=0;$i<4;$i++) 
  4.   echo "$people[$i] "
  5. ?> 

除了使用了解循环次数的for循环以外,还可以使用对循环次数无须要求的foreach语句:

  1. <?php 
  2. $people=array('name','sex','nation','birth'); 
  3. foreach($people as $xiangmu
  4.   echo $xiangmu
  5. ?> 

$xiangmu变量将保存数组中的各元素值,依次显示出来,当然,为了输出的数据能间隔区分,可在数组元素之后输出空格:

echo $xiangmu." ";现在来看一款php从数组里筛选出重复的数据:

  1. <?php 
  2. $num = count($array); 
  3. if($num
  4. sort($array); 
  5. if($num!=0) 
  6. $m = $array[0]; 
  7. $n = 0; 
  8. $kind=1; 
  9. echo $array[0]."———-"
  10. for($z=0;$z<$num;$z++) 
  11.  if($m!=$array[$z]) 
  12.  { 
  13.   echo $array[$z-1]." "
  14.   echo $array[$z]; 
  15.   $kind=0; 
  16.   $m=$array[$z]; 
  17.  } 
  18.  $n++; 
  19.  $m++; 
  20. //echo $array[$z]."'/n'"; 
  21. if($kind==1) 
  22.  echo $array[$num-1]."重复的数据! "
  23. ?> 
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php 数组的定义和初始化学习笔记

88 评论

  1. buy fildena online cheap methocarbamol usa buy robaxin 500mg generic

  2. purchase suhagra pill estrace brand purchase estradiol generic

  3. indocin drug buy trimox 250mg online cheap

  4. original tadalafil 10mg rezeptfrei sicher kaufen tadalafil generika sildenafil 200mg generika rezeptfrei kaufen

  5. order generic provigil 200mg cost provigil 200mg acetazolamide without prescription

  6. order benicar 10mg pills calan 240mg usa purchase diamox pills

  7. order zoloft 50mg online cheap brand zoloft 100mg order sildenafil 50mg pills

  8. buy cialis 40mg online order cialis amantadine canada

  9. order nootropil viagra 50mg us overnight delivery viagra

  10. cialis overnight shipping usa tadalafil liquid sildenafil 50mg for sale

  11. order allopurinol 100mg online crestor 20mg pills order generic ezetimibe 10mg

  12. purchase motilium generic sumycin pills flexeril drug

  13. college paper help paper writer ivermectin uk

  14. order furosemide 40mg for sale buy lasix pill plaquenil pills

  15. order sildenafil 50mg pill budesonide cheap buy generic rhinocort

  16. tadalafil 10mg brand brand indocin cost indomethacin 75mg

  17. female cialis tadalafil Buy cialis on line purchasing cialis on the internet

  18. carvedilol without prescription oxybutynin 5mg us amitriptyline 50mg price

  19. purchase doxycycline without prescription buy aralen pills methylprednisolone 8 mg over counter

  20. buy tricor generic tricor ca sildenafil in usa

  21. zofran cheap zofran sale buy sulfamethoxazole pill

  22. buy avodart 0.5mg pill xenical usa buy orlistat

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡