We all realize the advantages of arrays, now we will learn how to traverse the data in the array in PHP. 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. Of course, PHP will have a simple way. Let’s look at an example first:
<ol class="dp-xml"> <li class="alt"><span><span>foreach </span></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><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>'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>foreach($urlas$link){ </span></li> <li class="alt"> <span>echo$link.'</span><strong><font color="#006699"><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=""><span> </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
In the above code, we used the foreach statement, which is the simplest and most effective method for PHP to traverse data in an array.
foreach can only be used on arrays. An error will occur when trying to use it on other data types or on an uninitialized variable. There are two syntaxes, the second being a less important but useful extension of the first.
PHP array traversal syntax:
<ol class="dp-xml"><li class="alt"><span><span>foreach(array_expressionas$value)statement </span></span></li></ol>
In the $url array definition, "Sina", "NetEase", etc. are all key names instead of array values. Our first syntax just loops to obtain each value in the array.