Experience Sharing: PHP array loop fetch data tips _php Tutorial

Source: Internet
Author: User
We want to get a lot of data, you're going to loop the array, and we'll look at it now. PHPThe array loops 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?

 
  
  1. My favourite bands is:
    • Loop over it and print array elements for ($x = 0; $x < sizeof ($artists

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.

 
  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);?>

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:

 
  
  1. foreach ($array as $tempdo

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:

 
  1. < HTML > <head> head > < body> My favourite bands are: <ul> php
  2. //define array $ Artists = Array
  3. (' Metallica ', ' Evanescence ', ' Linkin Park ', ' Guns N Roses ');
  4. Loop over it//print array elements foreach ($artists as $a)
  5. {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/445746.html www.bkjia.com true http://www.bkjia.com/PHPjc/445746.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 ...

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