Ways to iterate through arrays in PHP

Source: Internet
Author: User
Tags php tutorial

Reference URL: http://www.jb51.net/article/29949.htm

The most efficient of these three methods is the use of a foreach statement to iterate over an array. The foreach structure has been introduced since PHP4, which is a statement designed specifically to iterate over arrays in PHP, and is recommended for everyone. There are three common ways to iterate through an array in PHP, including the following:
First, iterate through the array using the For statement;
Second, use the foreach statement to iterate through the array;
Third, the union uses the list (), each () and the while loop to iterate through the array.
The most efficient of these three methods is the use of a foreach statement to iterate over an array. The foreach structure has been introduced since PHP4, which is a statement designed specifically to iterate over arrays in PHP, and is recommended for everyone. First, we introduce these methods separately.

To iterate through an array using a For statement
It is worth noting that iterating through an array with a For statement requires that the array to be traversed must be an indexed array. There are not only associative arrays but also indexed arrays in PHP, so it is very rare to iterate through an array with a for statement in PHP.
The instance code is as follows:
Copy CodeThe code is as follows:
<?php
$arr = Array (' http://www.jb51.net ', ' home of the script ', ' php tutorial ');
$num = count ($arr);
for ($i =0; $i < $num; + + $i) {
echo $arr [$i]. ' <br/> ';
}
?>
Note: In the example code, we first calculate the number of elements in the array $arr, and then use it in the For statement, which is efficient. Because if it is for ($i =0, $i < count ($arr), + + $i), each loop calculates the number of elements in the array $arr, which can be subtracted by using the above method. Use + + $i is also to improve efficiency, there are articles we mentioned before, we suggest you look again.
The output of the above code is:
Http://www.jb51.net
Home of the script
PHP Tutorials

second, use the foreach statement to iterate through the array
There are two ways to iterate through an array using a foreach statement, and the most we use is the first way. Introduced as follows:
The first way:
foreach (array_expression as $value) {
Loop body
}
Instance code:
Copy CodeThe code is as follows:
<?php
$arr = Array (' http://www.jb51.net ', ' home of the script ', ' php tutorial ');
foreach ($arr as $value) {
echo $value. ' <br/> ';
}
?>

In each loop, the value of the current element is assigned to the variable $value, and the pointer inside the array is moved backward one step. So the next loop will get the next element of the array until the end of the array stops looping, ending the traversal of the array.

The second way:
foreach (array_expression as $key = = $value) {
Loop body
}
Instance code:
Copy CodeThe code is as follows:
<?php
Defining arrays
$arr = Array (' http://www.jb51.net ', ' home of the script ', ' php tutorial ');
foreach ($arr as $k = = $v) {
echo $k. " = ". $v." <br/> ";
}
?>

third, use the list (), each () and while loop to iterate through the array
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.

Instance code:
Copy CodeThe code is as follows:
<?php
Defines an array of loops
$arr = Array (' website ' = ' http://www.jb51.net ', ' webname ' = ' script home ')
while (list ($k, $v) = each ($arr)) {
echo $k. ' = '. $v. ' <br/> ';
}
?> jb51.net
The output is:
Website=>http://www.jb51.net
webname=>php Programmer

Summary: The above three methods of looping through arrays are recommended to iterate through arrays using a foreach statement, which is more efficient.

Ways to iterate through arrays in PHP

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.