There are three common ways to iterate through arrays in PHP

Source: Internet
Author: User
Tags php tutorial

The most manipulated data in PHP is the array, which has the characteristics of high efficiency, fast speed and convenient storage.

There are three common ways to iterate through an array in PHP:
1, for loop, use the most flexible, flexible to suspect life, but remember the format is very simple.
2, foreach, PHP is an array of traversal-specific functions, introduced in the PHP4 version, while the execution of the most efficient
3. Use the list (), each () and while loop to iterate through the array, which is used less, but the list () function uses very much

Look directly at the instance code:

<?PHP$ARR1 = Array (' http://www.jinsanguo.com/', ' Gold Three ', ' PHP tutorial '); $num = count ($arr),//count () is an array statistic function for ($i =0; $i < $num; + + $i) {            echo $arr [$i];         } $arr 2 = Array (' http:/ www.jinsanguo.com/', ' Golden Kingdoms ', ' php tutorial ');         foreach ($arr 2 as $value) {          echo $value;         } $arr 3 = Array (' http://www.jinsanguo.com/', ' Gold Three ', ' PHP tutorial ');       while (list ($key, $value) = each ($arr 3)) {          echo $key. ' = '. $value; }?>

each () function needs to pass an array as a parameter, return the key/value pair of the current element in the array, and move the array pointer backwards to the next element's position.
The list () function, which is not a real function, is a language structure of PHP. List () assigns a set of variables in one-step operation.

There are three common ways to iterate through arrays in PHP

Related Article

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.