Home > Backend Development > PHP Tutorial > A brief analysis of how to use PHP to traverse arrays_PHP Tutorial

A brief analysis of how to use PHP to traverse arrays_PHP Tutorial

WBOY
Release: 2016-07-15 13:27:46
Original
855 people have browsed it

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>
Copy after login

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>
Copy after login

This format traverses the given array_expression array, as is the keyword. Each time through the loop, the value of the current cell is assigned to $value and the pointer inside the array is moved forward one step (so the next cell will be obtained in the next loop). statement is a subsequent operation. If there are multiple statements, you can use curly brackets like the example above. In the above example, we read each unit in the array $url in a loop, and assign the value of the unit to the variable $link each time, and then echo prints out the value of $link, and then performs the next loop until the array There is no more data in . Have you tried it? If you try it, wouldn’t it be interesting to see the output? I know that after a while you will have questions again: Why are there no website names like "Sina" and "NetEase"? What do you need to do to get it?

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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446503.htmlTechArticleWe 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, I believe you will master it...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template