仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 626|回复: 7
打印 上一主题 下一主题

[学习教程] PHP网页编程之php之foreach遍历数组

[复制链接]
谁可相欹 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:11:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
基础这个工具是个比较笼统的概念,如果你之前学习过c语言,c语言被认为是foreach

(PHP4,PHP5)
Theforeachconstructprovidesaneasywaytoiterateoverarrays.foreachworksonlyonarraysandobjects,andwillissueanerrorwhenyoutrytouseitonavariablewithadifferentdatatypeoranuninitializedvariable.Therearetwosyntaxes:
  1. foreach(array_expressionas$value)statementforeach(array_expressionas$key=>$value)statement
复制代码
Thefirstformloopsoverthearraygivenbyarray_expression.Oneachiteration,thevalueofthecurrentelementisassignedto$valueandtheinternalarraypointerisadvancedbyone(soonthenextiteration,youllbelookingatthenextelement).
Thesecondformwilladditionallyassignthecurrentelementskeytothe$keyvariableoneachiteration.
Itispossibletocustomizeobjectiteration.
Note:
Whenforeachfirststartsexecuting,theinternalarraypointerisautomaticallyresettothefirstelementofthearray.Thismeansthatyoudonotneedtocallreset()beforeaforeachloop.
Asforeachreliesontheinternalarraypointer,changingitwithintheloopmayleadtounexpectedbehavior.
Inordertobeabletodirectlymodifyarrayelementswithintheloopprecede$valuewith&.Inthatcasethevaluewillbeassignedbyreference.
Referencing$valueisonlypossibleiftheiteratedarraycanbereferenced(i.e.ifitisavariable).Thefollowingcodewontwork:
WarningReferenceofa$valueandthelastarrayelementremainevenaftertheforeachloop.Itisrecommendedtodestroyitbyunset().
Note:
foreachdoesnotsupporttheabilitytosuppresserrormessagesusing@.
Youmayhavenoticedthatthefollowingarefunctionallyidentical:
Thefollowingarealsofunctionallyidentical:
Somemoreexamplestodemonstrateusage:
Unpackingnestedarrayswithlist()

(PHP5>=5.5.0)
PHP5.5addedtheabilitytoiterateoveranarrayofarraysandunpackthenestedarrayintoloopvariablesbyprovidingalist()asthevalue.
Forexample:
Theaboveexamplewilloutput:
  1. A:1;B:2A:3;B:4
复制代码
Youcanprovidefewerelementsinthelist()thanthereareinthenestedarray,inwhichcasetheleftoverarrayvalueswillbeignored:
Theaboveexamplewilloutput:
  1. 13
复制代码
Anoticewillbegeneratediftherearentenougharrayelementstofillthelist():
Theaboveexamplewilloutput:
  1. Notice:Undefinedoffset:2inexample.phponline7A:1;B:2;C:Notice:Undefinedoffset:2inexample.phponline7A:3;B:4;C:
复制代码
多个成员之间重复做相同的工作,很容易因为交流沟通的时候没有进行一致性的文档要求而出现不明错误,严重影响开发进度,导致在预定时间内无法完成该项目或者完成的项目跟原先计划所要实现的项目功能不符合。
愤怒的大鸟 该用户已被删除
沙发
发表于 2015-1-18 22:21:29 | 只看该作者
说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。
海妖 该用户已被删除
板凳
发表于 2015-1-25 13:44:22 | 只看该作者
有位前辈曾经跟我说过,phper 至少要掌握200个函数 编起程序来才能顺畅点,那些不熟悉的函数记不住也要一拿手册就能找到。所以建议新手们没事就看看php的手册(至少array函数和string函数是要记牢的)。
再现理想 该用户已被删除
地板
发表于 2015-2-2 22:19:01 | 只看该作者
学好程序语言,多些才是王道,写两个小时代码的作用绝对超过看一天书,这个我是深有体会(顺便还能练打字速度)。
简单生活 该用户已被删除
5#
发表于 2015-2-8 14:05:17 | 只看该作者
当然这种网站的会员费就几十块钱。
谁可相欹 该用户已被删除
6#
 楼主| 发表于 2015-3-8 01:12:03 | 只看该作者
真正的方向了,如果将来要去开发团队,你一定要学好smarty ,phplib这样的模板引擎,
柔情似水 该用户已被删除
7#
发表于 2015-3-15 19:20:28 | 只看该作者
Ps:以上纯属原创,如有雷同,纯属巧合
admin 该用户已被删除
8#
发表于 2015-3-22 03:10:30 | 只看该作者
说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年  具体的记不清啦,囧。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-6-10 17:51

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表