php中数组遍历方法有很多种,如有:for,foreach,while(),list(),each()这些函数与方法都要我们在数组遍历中会用到的,下面给大家介绍一下.
1. 使用for语句循环遍历数组
a. 其它语言(只有这一种方式)
b. PHP中这种方式不是我们首选方式
c. 数组必须是索引数组,而且下标还必须是连续的 索引数组下标还可以不连序,数组还有关联数组)
PHP实例代码如下:
- <?php
- /*
- * 我们想要在遍历的过程中改变以下数组中某些元素的值
- */
- $people = Array(
- Array(’name’ => ‘Kalle’, ’salt’ => 856412),
- Array(’name’ => ‘Pierre’, ’salt’ => 215863)
- );
- for($i = 0; $i < sizeof($people); ++$i)
- {
- $people[$i]['salt'] = rand(000000, 999999);
- }
- ?>
以上代码的问题,在于for的第二个表达式会导致代码执行很慢--因为每次循环时都要 计算一遍数组的长度,由于数组的长度始终不变,我们可以用一个中间变量来存储数组长度,然后用这个变量作为for循环的第二个表达式,这样在循环的时候就可以直接使用该变量的值,不用每次重新计算,代码如下:
- <?php
- $people = Array(
- Array(’name’ => ‘Kalle’, ’salt’ => 856412),
- Array(’name’ => ‘Pierre’, ’salt’ => 215863)
- );
- for($i = 0, $size = sizeof($people); $i < $size; ++$i)
- {
- $people[$i]['salt'] = rand(000000, 999999);
- }
- ?>
2.使用foreach语句循环遍历数组
- foreach(数组变量 as 变量值){
- 循环体
- }
a.循环次数由数组的元素个数决定
b.每一次循环都会将数组中的元素分别赋值给后面变量
- foreach(数组变量 as 下标变量=> 值变量){
- }
foreach遍历3维数组:
- //foreach循环一个3维数组
- /*
- $biaoge=array(
- "市场部"=>array(
- array(1,"gaomou1","jingli11",4000),
- array(2,"gaomou2","jingli22",4000),
- array(3,"gaomou3","jingli33",4000)
- ),
- "客服部"=>array(
- array(1,"gao1","li11",4000),
- array(2,"gao2","li22",4000),
- array(3,"gao3","li33",4000)
- ),
- "业务部"=>array(
- array(1,"mou1","jing11",4000),
- array(2,"mou2","jing22",4000),
- array(3,"mou3","jing33",4000)
- )
- );
- foreach($biaoge as $key=>$value){
- echo '<table align="center" width="600" border="1">';
- echo '<caption>联系表</caption>';
- echo '<tr bgcolor="#dddddd">';
- echo '<th>标号</th><th>名字</th><th>职位</th><th>工资</th></tr>';
- foreach($value as $row){
- if($row%2==0){
- $bg="#ffffff";
- }else{
- $bg="#dddddd";
- }
- echo '<tr bgcolor='.$bg.'>';
- foreach($row as $col){
- echo '<td>'.$col.'</td>';
- }
- echo '</tr>';
- }
- echo '</table>';
- }
- echo "<pre>";
- print_r($biaoge);
- echo "</pre>";
3. while() list() each() 组合循环遍历数组
each()函数.
a. 需要一个数组作为参数
b. 返回来的也是一个数组
c. 返回来的数组是0, 1, key, value四个下标(固定的)
0和key下标是当前参数数组元素的?
1和value下标是当前对数数组元素的值
d. 默认认当前元素就是第一个元素
e. 每执行一次后就会将当前元素向后移动
f. 如果到最后的元素再执行这个函数,则返回false
使用each遍历数组的示例代码如下:
- <?php
- //使用each函数遍历数组
- $arrGoogle=array('google','Gmail','Chrome','Android');
- //第一次使用each取得当前键值对,并且将指针移到下一个位置
- $arrG=each($arrGoogle);
- //打印结果,并且换行以清晰显示结果
- print_r($arrG);
- print '<br>';
- $arrGmail=each($arrGoogle);
- print_r($arrGmail);
- print '<br>';
- $arrChrome=each($arrGoogle);
- print_r($arrChrome);
- print '<br>';
- $arrAndroid=each($arrGoogle);
- print_r($arrAndroid);
- print '<br>';
- //当指针位于数组末尾再次执行函数each,如果是这样再次执行结果返回false
- $empty=each($arrGoogle);
- //如果指针无法继续后移返回false
- if($empty==false){
- print '指针以位于数组末尾,无法在向后移,故返回false';
- }
- ?>
注意:该函数的参数和返回值(在执行该函数前指针不位于数组末尾时)都为数组,在执行函数前数组指针位于数组末尾时再次执行该函数返回值为false,开始的位置是第一个元素,每(正常)执行一次该函数,指针向后移到下一个地址.
list()函数
a. list()=array(); 需要将一个数组赋值给这个函数
b. 数组中的元素个数,要和list()函数中的参数个数相同
c. 数组中的每个元素值会赋值list()函数中的每个参数,list()将每个参数转为变量
d. list()只能接收索引数组
e. 按索引的下标的顺序
这不是真正的函数,而是PHP的语言结构,list()用一步操作给一组变量进赋值,即把数组中的值赋给一些变量,list()仅能用于数字索引的数组并假定数字索引从0开始,语法格式如下所示:
list(mixed varname,mixed …)=array_expression //list()语句的语法格式
list()语句和其它函数在使用上有很大的区别,并不是直接接收一个数组作为参数,而是通过“=”去处符以赋值的方式,将数组中每个元素的值,对应的赋给list()函数中的每个参数,list()函数又将它中的每个参数转换为直接可以在脚本中使用的变量,使用方法如下:
- <?php
- $info=array('coffee','brown','caffeine'); //声明一个索引数组$info
- list($drink,$color,$power)=$info; //将数组中的所有元素转为变量
- echo "$drink is $color and $power makes it special.n";//输出的三个变量值是数组中三个元素的值
- list($drink, ,$power)=$info; //将数组中的部分元素变为变量
- echo "$drink has $power.n"; //输出的两个变更值是数组中的前两个元素的值
- list(,,$power)=$info; //跳过前两个变量只将数组中第三个元素的值转为变量
- echo "I need $power!n"; //输出的一个变量值是数组中桃花汛三个元素的值勤
- ?>
通过上例了解list()函数的用法之后,将each()函数和list()函数结合起来使用,代码如下:
- <?php
- $contact=array("ID"=>1,"姓名"=>"高某","公司"=>"A公司","地址"=>"北京市");
- list($key,$value)=each($contact); //将each()函数和list()函数联合使用
- echo "$key=>$value"; //输出变量$key和$value,中间使用"=>"分隔
- ?>
while() 函数
while()循环的语法格式如下:
- while( list($key,$value) = each(array_expressin) ){
- 循环体;
- }
使用这种组合改写前面使用foreach遍历过的一维数组,代码如下所示:
- <?php
- //声明一个一维的关联数组$contact
- $contact=array("ID"=>1,
- "姓名"=>"高某",
- "公司"=>"A公司",
- "地址"=>"北京市",
- "电话"=>"(010)987665432",
- "EMAIL"=>"gao@php.com"
- );
- //以HTML列表的方式输出数组中每个元素的信息
- echo '<dl>一个联系人信息:';
- while(list($key,$value) = each($contact) ){ //将foreach语句改写成while,list()和each()的组合
- echo "<dd>$key:$value</dd>"; //输出每个元素的键/值勤
- }
- echo '</dl>';
- ?>
while() 遍历数据是需要结合list或each函数配置才可以遍历,否则它单独是无法完成数组遍历的.
波比源码 » php数组的遍历函数与方法例子
levaquin 500mg cost buy levofloxacin
dutasteride canada buy tamsulosin sale zofran price
cost spironolactone 100mg order aldactone 100mg without prescription buy fluconazole 200mg pill
buy generic ampicillin cephalexin cheap order generic erythromycin 500mg
sildenafil 100mg sale brand careprost robaxin for sale
order suhagra 100mg online order aurogra 100mg sale order estrace 1mg sale
order lamotrigine generic cheap tretinoin buy retin online cheap
buy tadalis 20mg tadalafil 20mg usa voltaren 100mg uk
isotretinoin sale cheap amoxil generic zithromax without prescription
order indomethacin online cheap buy suprax 200mg online cheap purchase trimox online
tadalafil 40mg without prescription Real viagra pharmacy prescription order sildenafil 50mg generic
purchase arimidex without prescription purchase biaxin without prescription cheap generic sildenafil
tadalafil 10mg bestellen original viagra 200mg rezeptfrei sicher kaufen sildenafil kaufen ohne rezept
order modafinil 200mg order provigil sale buy acetazolamide generic
purchase doxycycline sale purchase levitra pill lasix 40mg usa
buy altace 10mg generic purchase avapro online buy azelastine generic
order clonidine online buy catapres 0.1 mg buy tiotropium bromide 9 mcg pills
alendronate 35mg without prescription fosamax buy online buy famotidine 20mg
order olmesartan without prescription benicar pill diamox 250 mg canada
buy prograf 1mg tacrolimus for sale buy urso 300mg pills
order bupropion generic bupropion 150 mg uk quetiapine 100mg oral
molnupiravir 200 mg ca purchase naprosyn without prescription prevacid 30mg us
cost imuran 100 mcg sildenafil pill viagra 50mg for sale
cialis over the counter fluoxetine 20mg cheap cheap sildenafil pill
cialis generic name order tadalafil without prescription buy amantadine 100mg sale
medroxyprogesterone uk order medroxyprogesterone 5mg without prescription order cyproheptadine 4 mg without prescription
provigil 200mg generic order stromectol 3mg pills buy ivermectin 2mg
buy fluvoxamine sale glipizide 5mg over the counter glucotrol 10mg without prescription
order isotretinoin 20mg online cheap buy accutane 10mg generic order prednisone 10mg online
order azithromycin 500mg for sale generic prednisolone 20mg oral neurontin 100mg
tadalafil 20mg drug cheap cialis without prescription viagra 50mg pills
buy furosemide online oral doxycycline 100mg plaquenil sale
sporanox 100 mg ca itraconazole buy online buy tindamax generic
purchase norvasc generic cost viagra 100mg tadalafil generic name
purchase clozapine for sale order dexamethasone 0,0,5 mg without prescription dexamethasone price
buy viagra generic buy lisinopril 2.5mg sale order lisinopril 10mg generic
lopressor price atenolol online order buy vardenafil pill
essay edit online poker free play blackjack online for real money
cheap custom research papers buying essay order viagra 50mg sale
buy clomiphene online cheap win real money online casino casino gambling
generic aristocort 4mg order loratadine pills oral desloratadine 5mg
order priligy 90mg generic buy misoprostol 200mcg pills synthroid 75mcg canada
order cialis 40mg without prescription order inderal 20mg generic clopidogrel price
order generic methotrexate 5mg methotrexate over the counter buy metoclopramide 20mg online
domperidone for sale online motilium 10mg uk buy flexeril online
best casino games casino arizona play poker online free no sign up
brand tamsulosin 0.2mg order flomax spironolactone us
flagyl oral bactrim over the counter buy trimethoprim pill
order cefuroxime 500mg pills order cefuroxime 250mg sale robaxin usa
trazodone cheap buy suhagra 50mg sale aurogra 100mg canada
affordable essay writing pay for dissertation ivermectin lotion price
sildalis price order estradiol 1mg sale lamictal uk
viagra 200mg canadian viagra and healthcare canadian cialis and healthcare
casino real money can you buy ed pills online tadalafil 20mg us
best no deposit free spins casino slot games order provigil 100mg for sale
buy generic furosemide 100mg cheap furosemide buy hydroxychloroquine generic
prednisone canada vermox order buy vermox 100mg without prescription
buy generic retin buy avanafil 200mg pills purchase avana
tadalafil 10mg us buy tadalafil sale indomethacin 50mg usa
buy lamisil 250mg without prescription brand trimox 500mg trimox uk
actos usa buy actos 15mg pill viagra brand
order cialis 10mg online cheap gambling casinos can you play poker online money
poker online for real money real online gambling empire casino online
real casino games gambling casinos hard rock casino online
brand ramipril 5mg order etoricoxib 120mg sale buy etoricoxib pill
doxycycline generic purchase albuterol generic buy cleocin online cheap
generic acetazolamide 250mg acetazolamide ca azathioprine 50mg brand
buy priligy generic avana oral domperidone ca
nortriptyline 25 mg for sale order paracetamol 500mg sale purchase paxil for sale
indomethacin 50mg usa cenforce 50mg over the counter buy cenforce sale
Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Onwin , Onwin Giriş , Onwin Güncel Giriş , Onwin Yeni Adres , onwin
cialis 10mg generic viagra 100mg brand sildenafil 100mg
buy esomeprazole 40mg capsules order esomeprazole 40mg without prescription furosemide without prescription
generic modafinil 200mg cost promethazine 25mg order promethazine 25mg pill
purchase prednisone for sale buy amoxicillin 250mg online order generic amoxicillin 500mg
accutane without prescription order ampicillin pill ampicillin online buy
stromectol 12 mg buy deltasone 40mg generic buy prednisone 40mg sale
purchase albuterol without prescription cost augmentin order augmentin 625mg online
buy isotretinoin 20mg for sale accutane tablet azithromycin pill
avodart over the counter dutasteride buy online order orlistat 120mg generic
purchase doxycycline levitra order online order acyclovir 400mg without prescription
order imuran 25mg online naproxen 500mg oral naproxen 500mg generic