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