Experience Sharing: PHP array data acquisition techniques. If we want to get a large pile of data, we need to loop through the array. now let's take a look at the PHP array loop to get the data. Because we are responsible for placing data in an array, now we need to loop through the array to see how we want to get a large pile of data.
PHPThe array loops to get the data. Because we are responsible for placing data in an array, how can we retrieve it now? Retrieving data from an array is very simple: all you need to do is to use the index number to access the appropriate elements of the array. To read the content of the entire array, you only need to use the loop structure you learned in chapter 3 of this tutorial to perform loop operations on it.
How about a quick example?
- My favourite bands are:
- // define array $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses');
- // loop over it and print array elements for ($x = 0; $x < sizeof($artists
When you run the script, you will see the following results:
- My favourite bands are: Metallica Evanescence Linkin Park Guns n Roses
In this example, I first define an array, and then use the for () loop to do the following: traverse the array and use the index symbol to retrieve elements, 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. Returns the size of the array (read: number of elements in the array ). It is mostly used for loop counters to ensure that the number of loops is consistent with the number of all elements in the array. If you are using a union array, you can use the array_keys () and array_values () functions to obtain a list of all the keywords and corresponding values in the array.
- '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 "
- "; /* returns the array ('bacon and eggs', 'roast beef', 'lasagna') with numeric indices */
- $result = array_values($menu); print_r($result); ?>
However, there is also a simpler method to extract all elements in the array. PHP4.0 introduces a very new loop type designed specifically for repeated array enumeration: foreach () loop (its syntax structure is similar to the Perl structure of the same name ).
The syntax format is as follows:
- foreach ($array as $temp) { do this! }
The foreach () loop runs once every element of the array passed to it as a parameter, traversing the array forward at each repetition. Unlike a for () loop, it does not need a counter or call the sizeof () function because it automatically tracks its position in the array. During each running, execute the statement in braces. at the same time, the selected array element can be accessed through a temporary PHP array loop variable. To better understand how it works, consider using the foreach () loop to rewrite the previous example:
- My favourite bands are:
- // define array $artists = array
- ('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses');
- // loop over it // print array elements foreach ($artists as $a)
- { echo '
- '.$a; } ?>
Each time a loop is executed, it places the value of the currently selected array element in the temporary variable $. The variable can be used by statements in the PHP array loop block. Because a foreach () loop does not require a counter to track its position in an array, it requires less maintenance and is easier to read than a standard for () loop. Oh, yes ..., It can also work with associated arrays without additional programming.
The PHP array cyclically obtains data. Because we are responsible for placing data in an array, how can we put...