We want to get a lot of data, you're going to loop through the arrays, and we're going to look at the PHP array loop to get the data. Now, how do you take the data out of the array? Retrieving data from an array is very simple: all you have to do is use the index number to access the appropriate elements of the array. In order to read the contents of the entire array, you simply need to loop through the loop structure you learned in chapter three of the tutorial.
How about a quick example?
- <html> <head> head> <body> My favourite bands is: <ul> php
Define Array $Artists = Array(' Metallica ', ' Evanescence ', ' Linkin Park ', ' Guns N Roses ');
Loop over it and print array elements for ($x = 0; $x< sizeof($artists); $x + +) {echo ' <li> '. $artists [$x];} ?> ul> body> html>
When you run the script, you will see the following result:
In this example, I first define an array and then use the for () loop to do the following: Iterate through the array, use the index symbols 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. The size of the returned array (read: The number of elements in the array). Most of it is used in loop counters to ensure that the number of loops and all elements in the array is the same. If you are using a federated array, you can get the Array_keys () and array_values () functions handy to give you a list of all the keywords and corresponding values in the array.
- php //Define an array $Menu = Array(' breakfast ' =>' 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 " <BR />"; /* Returns the array (' bacon and eggs ', ' roast beef ', ' lasagna ') with numeric indices */$result = array_values($menu);
Print_r ($result); ?>
There is, however, an easier way to extract all the elements in an array. PHP4.0 describes a very new type of loop that is designed specifically for the purpose of repeated enumeration of arrays: the foreach () loop, which has a syntax structure similar to the Perl structure of the same name.
Here is its syntax format:
The foreach () loop runs once for each element of an array passed as a parameter to it, and iterates forward through the array each time it repeats. Unlike for () loops, it does not require a counter or call function sizeof () because it automatically tracks its position in the array. At each run, the statement inside the curly braces is executed, and the currently selected array element can be accessed through a temporary PHP array loop variable.
To better understand how it works, consider rewriting the previous example using the foreach () Loop:
- <html> <head> head> <body> My favourite bands is: <ul> php //define array $Artists = Array
(' Metallica ', ' Evanescence ', ' Linkin Park ', ' Guns N Roses ');//loop over it//print array elements foreach ($artist s as $a)
{echo ' <li> '. $a;} ?> ul> body> html>
Each time the loop is executed, it places the value of the currently selected array element in the temporary variable $ A. The variable can then be used by the statements in the PHP array loop block. Because the foreach () loop does not require a counter to track its position in the array, it requires less maintenance and is easier to read than the standard for () loop. Oh, yes ... it can also work with associative arrays without the need for additional programming.
http://www.bkjia.com/PHPjc/446462.html www.bkjia.com true http://www.bkjia.com/PHPjc/446462.html techarticle we want to get a lot of data, you're going to loop through the arrays, and we're going to look at the PHP array loop to get the data. To be responsible for putting the data inside the array, now, how to put it ...