Experience Sharing: PHP array loop data acquisition tips _ PHP Tutorial

Source: Internet
Author: User
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?

 
 
  1. 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:

 
 
  1. 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.

 
 
  1. 'bacon and eggs', 'lunch' => 'roast beef', 'dinner' => 'lasagna');
  2. /* returns the array ('breakfast', 'lunch', 'dinner') with numeric indices */
  3. $result = array_keys($menu); print_r($result); print "
  4. "; /* returns the array ('bacon and eggs', 'roast beef', 'lasagna') with numeric indices */
  5. $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:

 
 
  1. 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:

 
 
  1. 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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.