以前我们经常会想怎么来实现php数组循环输出来,本文章来介绍了利用php中常用的四种循环语句来实现单数组或多维数组循环输出方法.
在 PHP 中,我们可以使用下列循环语句:
while
只要指定的条件成立,则循环执行代码块
do…while
首先执行一次代码块,然后在指定的条件成立时重复这个循环
for
循环执行代码块指定的次数
foreach
根据数组中每个元素来循环代码块
先我们利用php自带函数对php数组进行遍历
随手可得array_keys()和array_values()函数以用来得到数组中所有关键字和对应的值的列表.
实例代码如下:
- 'bacon and eggs', 'lunch' => 'roast beef', 'dinner' => 'lasagna');
- /* returns the array ('breakfast', 'lunch', 'dinner') with numeric indices */
- $result = array_keys($menu); print_r($result); print " ";
- /*
- returns the array ('bacon and eggs', 'roast beef', 'lasagna') with numeric indices */
- $result = array_values($menu); print_r($result);
- ?>
利用foreach 遍历php数组
PHP foreach() 语法结构用于遍历操作或输出数组,foreach() 仅能用于遍历数组或对象,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误.
实例代码如下:
- list = array('upid'=>'1','title'=>'公司新闻','list'=>array('id'=>'3','title'=>'公司新闻标题测试!','intime'=>'2009-29-5'));
- foreach ($list as $v=>$a)
- {
- echo $a['upid'] .'-'. $a['title'];
- foreach ($a['list'] as $b){
- echo $b['title'];
- }
- }
使用数组键值实例代码如下:
- <?php
- $arr_age = array("wang"=>18, "li"=>20, "zhang"=>25);
- foreach ($arr_age as $key=>$age) {
- echo $key,': ',$age,'<br />';
- }
- ?>
运行例子输出:
wang: 18
zhang: 25
当 foreach 开始执行时,数组内部的指针会自动指向第一个单元,这意味着不需要在 foreach 循环之前调用 reset().
for() 循环遍历数组
如果是操作连续键值的数组,也可以采用 for() 循环来遍历数组:
实例代码如下:
- <pre>
- <?php
- $shuzu=array("ni","wo","ta","php","mysql");
- $count=count($shuzu);
- echo "使用for遍历数组";
- echo "<br/>$nbsp;<br/>";
- for($i=0;$i<$count;$i++)
- {
- $j=$i+1;
- echo "第{$j}个元素是: $shuzu[$i]";
- echo "<br/>$nbsp;<br/>";
- }
- ?>
- </pre>
25
也可以用 list() 和 each() 结合来遍历php数组,但测试发现效率不如 foreach() .
list函数
list() 函数用数组中的元素为一组变量赋值.
注意,与 array() 类似,list() 实际上是一种语言结构,不是函数.
语法
list(var1,var2…)参数 描述
var1 必需.第一个需要赋值的变量.
var2 可选.可以有多个变量.
提示和注释
注释:该函数只用于数字索引的数组,且假定数字索引从 0 开始
*/
实例代码如下:
- $colors = array('red','blue','green','yellow');
- while(list($key,$val) = each($colors)) {
- echo "other list of $val.<br />";
- }
波比源码 » php数组循环输出实现方法
order levaquin for sale levaquin online
ampicillin for sale trimethoprim oral oral erythromycin
purchase sildenafil without prescription sildenafil 25mg for sale estradiol 2mg cost
isotretinoin 20mg cost generic azithromycin ivermectin cream cost
brand prograf urso 150mg brand ursodiol 300mg pills
purchase molnunat pill order naprosyn 500mg for sale lansoprazole sale
sertraline 100mg generic Viagra australia sildenafil drug
discount cialis Cialis store viagra 50mg pill
piracetam 800mg uk cheap viagra generic viagra 100mg for sale
furosemide price order lasix 40mg order plaquenil 400mg pill
clozapine 100mg cost buy ipratropium 100mcg without prescription dexamethasone 0,5 mg pill
order sildenafil 100mg without prescription order sildenafil online order lisinopril 10mg generic
prilosec generic prilosec 10mg generic real money casino app
buy zyvox 600mg pills linezolid without prescription best no deposit free spins
help with writing a paper vegas casino online casino online roulette real money
order priligy pill cytotec price buy synthroid 100mcg without prescription
tadalafil 20mg over the counter sildenafil 100mg ca viagra 50mg price
allopurinol drug cost ezetimibe 10mg purchase ezetimibe without prescription
purchase imitrex oral sumatriptan 50mg buy dutasteride online cheap
rivers casino world poker online real money spins
keflex tablet erythromycin order erythromycin 500mg generic
buy cialis 5mg for sale sildenafil for women order generic sildenafil 50mg
viagra price sildenafil next day cialis 20mg generic
gambling casinos cialis 20 cialis 40mg
buying essays order stromectol 12mg online buy ivermectin 12 mg
lasix 100mg ca lasix 100mg brand order hydroxychloroquine 200mg for sale
oral retin cream retin online buy buy generic avanafil
cost tadalafil 10mg order tadalafil generic buy indocin 75mg pills
buy naproxen 250mg pills purchase omnicef pills prevacid oral
order proventil 100 mcg pills order protonix 20mg online order cipro pills
buy tadalafil online cheap buy erectile dysfunction medications order tadalafil 20mg for sale
play blackjack online casino near me online black jack
buy generic altace order arcoxia buy etoricoxib 60mg pill
generic doxycycline ventolin over the counter cleocin 150mg tablet
purchase asacol online order astelin sprayers buy generic avapro 150mg
digoxin 250 mg cost molnupiravir price molnunat usa
order pepcid 20mg without prescription pepcid 20mg tablet buy remeron 15mg pill
and Escherichia coli in an anal gland Eykyn and Grace, 1986 can i buy cialis online
Good info. Lucky me I reach on your website by accident, I bookmarked it. Metropol Halı Karaca Halı Öztekin ve Selçuklu Halı Cami Halısı ve Cami Halıları Türkiye’nin En Büyük Cami Halısı Fabrikasıyız…
purchase nexium pill buy biaxin without prescription order lasix for sale
buy minocin 50mg online purchase neurontin sale hytrin 1mg usa
order modafinil 100mg online cheap order promethazine generic order phenergan for sale
cost prednisone 40mg cost deltasone 20mg amoxil 250mg brand
ventolin inhalator order levoxyl price brand augmentin 375mg
buy generic modafinil 200mg order provigil 100mg online cheap oral metoprolol
prednisolone 20mg tablet lasix cheap furosemide 40mg canada
avodart brand xenical for sale order generic orlistat
purchase imuran online cheap order telmisartan 20mg pills order naprosyn 500mg for sale
order cefdinir pills order lansoprazole 15mg pills buy protonix generic
dapsone 100mg brand order asacol 400mg online cheap order atenolol 50mg pills
order sildenafil online cheap sildenafil online oral cialis 20mg
order promethazine 25mg pills phenergan cost order cialis 20mg
Tamoxifen injections in О±MHC MerCreMer mice induce a loxP independent DNA damage response, leading to myocardial dysfunction and death brand name cialis online Two of Faldo s came at Muirfield in 1987 and 1992
levitra purchase Mood swings, achy joints that make it hard to want to exercise, and weight gain which is making me miserable, and the general malaise i always feel like I am on the verge getting a cold or the flu and really tired
купить справку в москве
Hello! I just want to give you a huge thumbs up for the great info you’ve got here on this post. I’ll be coming back to your blog for more soon.
Informative article, just what I wanted to find.
I am really glad to read this blog posts which includes lots of helpful data, thanks for providing these information.
I know this if off topic but I’m looking into starting my own blog and was wondering what all is required to get set up? I’m assuming having a blog like yours would cost a pretty penny? I’m not very internet savvy so I’m not 100% positive. Any recommendations or advice would be greatly appreciated. Thank you
you are in point of fact a just right webmaster. The web site loading velocity is incredible. It sort of feels that you are doing any unique trick. Moreover, The contents are masterpiece. you have performed a magnificent task in this topic!
Howdy! This post couldn’t be written any better! Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this post to him. Pretty sure he will have a good read. Thanks for sharing!
Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!
I was wondering if you ever considered changing the layout of your blog? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or two images. Maybe you could space it out better?
Hi there, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you prevent it, any plugin or anything you can advise? I get so much lately it’s driving me insane so any help is very much appreciated.
Hey there just wanted to give you a quick heads up and let you know a few of the images aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same results.
coli, a gene mutation test with Chinese hamster V79 cells, a cytogenetic test with Chinese hamster ovary cells, and a rat micronucleus test levitra vs viagra vs cialis
Hey! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a extraordinary job!
Hi there it’s me, I am also visiting this website regularly, this site is in fact nice and the people are truly sharing nice thoughts.
Wow, awesome blog layout! How long have you been blogging for? you make blogging glance easy. The entire glance of your site is magnificent, let alonesmartly as the content!
Can you tell us more about this? I’d like to find out more details.
Great goods from you, man. I’ve keep in mind your stuff prior to and you’re simply too fantastic. I really like what you’ve acquired here, really like what you’re stating and the best way by which you assert it. You make it entertaining and you still take care of to stay it smart. I can not wait to read far more from you. This is actually a great site.
Hi there to all, how is all, I think every one is getting more from this web site, and your views are pleasant for new viewers.
Thanks to my father who shared with me about this weblog, this webpage is really remarkable.
Saved as a favorite, I really like your site!
I have been surfing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my opinion, if all website owners and bloggers made good content as you did, the internet will be much more useful than ever before.
Amazing blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple adjustements would really make my blog jump out. Please let me know where you got your design. With thanks
Greate article. Keep writing such kind of information on your page. Im really impressed by your site.
Hi, its nice post concerning media print, we all know media is a great source of data.
My spouse and I stumbled over here different website and thought I might as well check things out. I like what I see so now i’m following you. Look forward to going over your web page yet again.
This is very fascinating, You are an excessively professional blogger. I have joined your feed and sit up for looking for more of your magnificent post. Also, I have shared your site in my social networks
With havin so much content and articles do you ever run into any problems of plagorism or copyright violation? My website has a lot of exclusive content I’ve either created myself or outsourced but it looks like a lot of it is popping it up all over the web without my agreement. Do you know any solutions to help stop content from being ripped off? I’d genuinely appreciate it.
I simply could not leave your site prior to suggesting that I really enjoyed the standard information a person supply on your visitors? Is going to be back often in order to check up on new posts
Wow, incredible blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is fantastic, let alone the content!
Hi there! Would you mind if I share your blog with my facebook group? There’s a lot of people that I think would really enjoy your content. Please let me know. Thanks
Hi there, I found your web site by means of Google whilst searching for a similar topic, your site got here up, it appears good. I have bookmarked it in my google bookmarks.
This is my first time visit at here and i am truly impressed to read all at one place.
What’s up it’s me, I am also visiting this site daily, this website is actually nice and the users are actually sharing pleasant thoughts.
Hi! I know this is somewhat off-topic but I had to ask. Does building a well-established blog like yours take a lot of work? I’m completely new to operating a blog but I do write in my diary daily. I’d like to start a blog so I will be able to share my experience and thoughts online. Please let me know if you have any suggestions or tips for new aspiring bloggers. Appreciate it!
Hi friends, its great piece of writing concerning tutoringand completely explained, keep it up all the time.
Howdy would you mind stating which blog platform you’re working with? I’m looking to start my own blog in the near future but I’m having a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique. P.S Apologies for getting off-topic but I had to ask!
Greate pieces. Keep writing such kind of information on your page. Im really impressed by your site.
I visited several web sites except the audio quality for audio songs current at this web site is in fact wonderful.