Home  >  Article  >  Backend Development  >  Experience Sharing: PHP Array Loop Data Fetching Techniques_PHP Tutorial

Experience Sharing: PHP Array Loop Data Fetching Techniques_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 10:57:54814browse

If we want to get a lot of data, you have to loop through the array. Let’s take a look at PHPlooping through the array to get the data. Since you're responsible for placing the data inside the array, now, how do you get it out? Retrieving data from an array is very simple: all you need to do is use the index number to access the appropriate element of the array. To read the contents of the entire array, you simply loop over it using the loop construct you learned in Chapter 3 of this tutorial.

How about a quick example?

<ol class="dp-c">
<li class="alt"><span><span><html> <head></head> <body> My favourite bands are: <ul> <?php   </span></span></li><li><span class="comment">// define array $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses');  </span><span> </span></li><li class="alt"><span class="comment">// loop over it and print array elements for ($x = 0; $x < sizeof($artists</span><span> </span></li></ol>

When you run the script, you will see the following results:

<ol class="dp-xml"><li class="alt"><span><span>My favourite bands are: Metallica Evanescence Linkin Park Guns n Roses </span></span></li></ol>

In this example, I first defined an array, and then used a for() loop to do the following: traverse the array, use index notation to get the elements, and then display them one by one. Here, I will draw your attention to the sizeof() function. This function is one of the most important and commonly used array functions. It returns the size of the array (read: the number of elements in the array). It is mostly used in loop counters to ensure that the number of loops is consistent with the number of all elements in the array. If you are using associative arrays, the array_keys() and array_values() functions are readily available to get a list of all the keys and corresponding values ​​in the array.

<ol class="dp-c"><li class="alt"><span><span class="string">'bacon and eggs'</span><span>, </span><span class="string">'lunch'</span><span> => </span><span class="string">'roast beef'</span><span>, </span><span class="string">'dinner'</span><span> => </span><span class="string">'lasagna'</span><span>);</span></span></li>
<li class="alt"><span><span> </span><span class="comment">/* returns the array ('breakfast', 'lunch', 'dinner') with numeric indices */</span><span> </span></span></li>
<li class="alt"><span><span class="vars">$result</span><span> = </span><span class="func">array_keys</span><span>(</span><span class="vars">$menu</span><span>); print_r(</span><span class="vars">$result</span><span>); print "   </span></span></li>
<li>
<span>"; </span><span class="comment">/* returns the array ('bacon and eggs', 'roast beef', 'lasagna') with numeric indices */</span><span> </span>
</li>
<li>
<span class="vars">$result</span><span> = </span><span class="func">array_values</span><span>(</span><span class="vars">$menu</span><span>); print_r(</span><span class="vars">$result</span><span>); ?> </span>
</li>
</ol>

However, there is an easier way to extract all elements in an array. PHP 4.0 introduces a very new type of loop designed specifically for the purpose of repeatedly enumerating arrays: the foreach() loop (its syntactic structure is similar to the Perl structure of the same name).

The following is its syntax format:

<ol class="dp-c"><li class="alt"><span><span class="keyword">foreach</span><span> (</span><span class="vars">$array</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$temp</span><span>) { </span><span class="keyword">do</span><span> this! } </span></span></li></ol>

The foreach() loop runs once for each element of the array passed to it as a parameter, repeating each time while traversing the array forward. Unlike the for() loop, it does not require a counter or calling the function sizeof() because it automatically keeps track of its position in the array. On each run, the statements within the curly braces are executed, and the currently selected array element is accessed through a temporary PHP array loop variable. To better understand how this works, consider a rewrite of the previous example using a foreach() loop:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span> </span><span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span> </span><span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span> My favourite bands are: </span><span class="tag"><</span><span class="tag-name">ul</span><span class="tag">></span><span> </span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li class="alt"><span><span>// define array $</span><span class="attribute">artists</span><span> = </span><span class="attribute-value">array</span><span>   </span></span></li><li><span>('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses'); </span></li><li><span>// loop over it // print array elements foreach ($artists as $a)   </span></li><li class="alt"><span>{ echo '</span><span class="tag"><</span><span class="tag-name">li</span><span class="tag">></span><span>'.$a; } </span><span class="tag">?></span><span> </span><span class="tag"></</span><span class="tag-name">ul</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li></ol>

Each time the loop is executed, it will select The value of the array element is placed in the temporary variable $a. This variable can then be used by statements within the PHP array loop block. Because the foreach() loop does not require a counter to keep track of its position in the array, it requires less maintenance and is more readable than a standard for() loop. Oh, yes... it also works with associative arrays, with no additional programming required.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445746.htmlTechArticleIf we want to get a large amount of data, you have to loop through the array. Let’s take a look at the PHP array now. Loop to get data. Since we are responsible for placing the data in the array, now, how to...
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