Home >Backend Development >PHP Tutorial >PHP array traversal syntax explained_PHP tutorial

PHP array traversal syntax explained_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-15 13:27:56985browse

PHP is a scripting language for WEB development. I think everyone has a deep understanding of it. Here I will discuss PHP array traversal with you. I hope it will be helpful to you.

We all realize the superiority of arrays, now we will learn how to traverse data in PHP array. The application of this method of operating arrays is very common, and I believe you will master it well. For example, your website stores a large amount of URL data. You want to display the portal URL on a page. These data have been saved in an array variable $url. So how do you output these data to the web page one by one? Do you still imagine echo outputting one by one like the previous lesson? Don’t forget that the amount of data is very large and the number of data changes frequently.

PHP array traversal syntax:

<ol class="dp-xml"><li class="alt"><span><span>foreach(array_expressionas$</span><span class="attribute"><font color="#ff0000">key</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$value)statement </span></span></li></ol>

The second format does the same thing, except that the key value of the current cell will also be traversed in each loop Assigned to variable $key.

For example 1:

<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> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>url</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('新浪'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span>'www.sina.com',  </span>
</li>
<li class="alt">
<span>'雅虎'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.yahoo.com',  </span>
</li>
<li class="">
<span>'网易'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.163.com',  </span>
</li>
<li class="alt">
<span>'腾讯'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.qq.com',  </span>
</li>
<li class="">
<span>'百度'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.baidu.com'  </span>
</li>
<li class="alt"><span>);  </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>foreach($urlas$</span><span class="attribute"><font color="#ff0000">site</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$link){  </span>
</li>
<li class="">
<span>echo"</span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>ahref</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>ahref</FONT></STRONG></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>http</FONT></SPAN><SPAN>://$link</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span>$site</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>a</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>";  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
<li class="alt"><span>$site得到键值(如“新浪”),$link得到该单元的值(如“www.sina.com”),我在中间夹了一些HTML代码,相信你会看懂的。下面我再加点东西你能看懂吗?例10-3:  </span></li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>url</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('新浪'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span>'www.sina.com',  </span>
</li>
<li class="">
<span>'雅虎'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.yahoo.com',  </span>
</li>
<li class="alt">
<span>'网易'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.163.com',  </span>
</li>
<li class="">
<span>'腾讯'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.qq.com',  </span>
</li>
<li class="alt">
<span>'百度'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'www.baidu.com'  </span>
</li>
<li class=""><span>);  </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">n</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;  </span>
</li>
<li class="">
<span>foreach($urlas$</span><span class="attribute"><font color="#ff0000">site</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$link){  </span>
</li>
<li class="alt"><span>++$n;  </span></li>
<li class="">
<span>echo"第$n名:</span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>ahref</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>ahref</FONT></STRONG></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>http</FONT></SPAN><SPAN>://$link</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span>$site</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>a</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>";  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>

Note: When foreach starts to execute, the pointer inside the array will automatically point to the first unit. This means there is no need to call reset() before the foreach loop. Note: Also note that foreach operates on a copy of the specified array, not the array itself. Therefore, even if there is an each() construct, the original array pointer does not change, and the value of the array unit is not affected. Note: foreach does not support the ability to use "@" to suppress error messages.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446498.htmlTechArticlePHP is a scripting language for WEB development. I think everyone has a deep understanding of it. Here I Let’s discuss with you about PHP array traversal. I hope it will be helpful to you. All of us...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn