快速掌握PHP数组循环取数据技巧_PHP教程

php中文网
发布: 2016-07-15 13:28:07
原创
1394人浏览过

我们想要得到大堆数据,你就要对数组进行循环,我们现在就来看看php数组循环得到数据。因要负责将数据放置在数组内,现在,如何将其取出呢? 从数组中检索数据非常简单:所有你所需要做的就是使用索引号来访问数组的适当元素。为了读取整个数组的内容,你只需要使用你在该教程第三章中所学到的循环结构来简单的对其进行循环操作即可。

来一个快捷的例子如何?

<OL class=dp-xml><LI class=alt><SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN><SPAN class=tag></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> My favourite bands are: </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>ul</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> <br>// define array $</SPAN><SPAN class=attribute><FONT color=#ff0000>artists</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses'); <br>// loop over it and print array elements for ($</SPAN><SPAN class=attribute><FONT color=#ff0000>x</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>0</FONT></SPAN><SPAN>; $x </SPAN><SPAN class=tag><STRONG><FONT color=#006699><</FONT></STRONG></SPAN><SPAN> </SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>sizeof</FONT></STRONG></SPAN><SPAN>($artists); $x++) { echo '</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>li</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>'.$artists[$x]; } </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>ul</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN></LI></OL>
登录后复制

当你运行该脚本时,你会看到下面的结果:

<OL class=dp-xml><LI class=alt><SPAN><SPAN>My favourite bands are: Metallica Evanescence Linkin Park Guns n Roses </SPAN></SPAN></LI></OL>
登录后复制

在这个实例中,我首先定义了一个数组,然后使用for()循环做下面的工作:遍历该数组,使用索引符号取得元素,然后一个接一个的显示它们。 在这里,我将吸引你们的注意力在sizeof()函数上。该函数是最重要也是最常用的数组函数之一。其返回数组的大小(读取:数组内元素的个数)。它大部分用于循环计数器中以确保循环次数和数组中的所有元素的个数一致。 如果你正在使用联合数组,那么随手可得array_keys()和array_values()函数以用来得到数组中所有关键字和对应的值的列表。
<OL class=dp-xml><LI class=alt><SPAN><STRONG><FONT color=#006699><SPAN class=tag><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> // define an array $</SPAN><SPAN class=attribute><FONT color=#ff0000>menu</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('breakfast' =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 'bacon and eggs', 'lunch' =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 'roast beef', 'dinner' =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN> 'lasagna'); <br>/* returns the array ('breakfast', 'lunch', 'dinner') with numeric indices */ $</SPAN><SPAN class=attribute><FONT color=#ff0000>result</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array_keys</FONT></SPAN><SPAN>($menu); print_r($result); <br>print "</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></FONT></STRONG></SPAN><SPAN>"; /* returns the array ('bacon and eggs', 'roast beef', 'lasagna') with numeric indices */ $</SPAN><SPAN class=attribute><FONT color=#ff0000>result</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array_values</FONT></SPAN><SPAN>($menu); <br>print_r($result); </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN></SPAN></LI></OL>
登录后复制

然而,这里还有一种更简单的方法来提取数组中的所有元素。PHP4.0介绍了一种经设计专门用于对数组反复枚举目的的非常新的循环类型:foreach()循环(它的语法结构类似于同名的Perl结构)。

立即学习PHP免费学习笔记(深入)”;

Linux+PHP+MySQL案例教程
Linux+PHP+MySQL案例教程

本书以培养高级网站建设与管理人才为目标,内容循序渐进,由浅入深,通过大量的实例系统全面地介绍了Linux+PHP+MySQL环境下的网络后台开发技术。本书详尽分析了近30个典型案例。包括计数器、网站流量统计、留言板、论坛系统、聊天室、投票与调查、用户管理、新闻发布系统、广告轮播、购物系统等等,力求让读者通过对案例的学习,轻松掌握PHP和MySQL的编程精要,迅速掌握网络后台开发技巧。   本书适

Linux+PHP+MySQL案例教程 466
查看详情 Linux+PHP+MySQL案例教程

下面是其语法格式:

<OL class=dp-xml><LI class=alt><SPAN><SPAN>foreach ($array as $temp) { do this! } </SPAN></SPAN></LI></OL>
登录后复制

foreach()循环对作为参数传递给它的数组的每一个元素运行一次,在每次重复时向前遍历该数组。和for()循环不同,它不需要计数器或调用函数 sizeof(),因为它自动跟踪其在数组中的位置。在每次运行的时候,执行大括号内的语句,同时,当前选择的数组元素可以通过一个临时的PHP数组循环变量来访问。

为了更好的理解它是如何工作的,考虑使用foreach()循环对之前的例子进行重新改写:

<OL class=dp-xml><LI class=alt><SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN><SPAN class=tag></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> My favourite bands are: </SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>ul</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> // define array $</SPAN><SPAN class=attribute><FONT color=#ff0000>artists</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array<br></FONT></SPAN><SPAN>('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses'); // loop over it // print array elements foreach ($artists as $a)<br>{ echo '</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>li</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN>'.$a; } </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></FONT></STRONG></SPAN><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>ul</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN><STRONG><FONT color=#006699><SPAN class=tag></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></SPAN></FONT></STRONG><SPAN> </SPAN></SPAN></LI></OL>
登录后复制

每次执行循环时,它将当前选择的数组元素的值放在临时变量$a中。之后,该变量可以被PHP数组循环块中的语句进行使用。因为foreach()循环不需要计数器跟踪其在数组中的位置,所以它需要更少的维护且同时比标准的for()循环更加易读。奥,是的…,它同样也可与关联数组一起起作用,而不需要额外的编程。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446462.htmlTechArticle我们想要得到大堆数据,你就要对数组进行循环,我们现在就来看看PHP数组循环得到数据。因要负责将数据放置在数组内,现在,如何将其...
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号