Several methods of traversing arrays in PHP _php tips

Source: Internet
Author: User
Tags php tutorial
There are three common ways to traverse an array in PHP:
Iterate through the array using a For statement;
Second, using the foreach statement to traverse the array;
The Union uses the list (), each (), and the while loop to iterate through the array.
The most efficient of the three methods is to traverse the array using the foreach statement. From PHP4 began to introduce a foreach structure, PHP is specifically designed to traverse the array of statements, recommended that everyone use. First, introduce these methods separately.

Iterate through an array using a For statement
It's worth noting that iterating through an array with a For statement requires that the array that is traversed must be an indexed array. In PHP, there are not only associative arrays but also indexed arrays, so PHP rarely loops through the array with a for statement.
The instance code is as follows:
Copy Code code as follows:

<?php
$arr = Array (' http://www.jb51.net ', ' cloud-dwelling community ', ' php tutorial ');
$num = count ($arr);
for ($i =0; $i < $num; + + $i) {
echo $arr [$i]. ' <br/> ';
}
?>

Note: In the previous example code, it is efficient to calculate the number of elements in the $arr of the array before using the For statement. Because if it is for ($i =0 $i < count ($arr) + + $i), each loop computes the number of elements in the array $arr, which can be subtracted from the overhead. Use + + $i is also to improve efficiency, the previous article we mentioned, we suggest that we look again.
The output from the above code is:
Http://www.jb51.net
Cloud-dwelling communities
PHP Tutorials

traversing an array using the foreach statement
There are two ways to iterate through an array by using the foreach statement, and the most we use is the first way. The introduction is as follows:
The first way:
foreach (array_expression as $value) {
Circulation body
}
Instance code:
Copy Code code as follows:

<?php
$arr = Array (' http://www.jb51.net ', ' cloud-dwelling community ', ' 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 back one step. So the next loop will get the next element of the array, until the end of the array stops looping, ending the array traversal.

The second way:
foreach (array_expression as $key => $value) {
Circulation body
}
Instance code:
Copy Code code as follows:

<?php
Defining arrays
$arr = Array (' http://www.jb51.net ', ' cloud-dwelling community ', ' php tutorial ');
foreach ($arr as $k => $v) {
echo $k. " => ". $v." <br/> ";
}
?>


the union uses the list (), each (), and the while loop to iterate through the array
The each () function needs to pass an array as a parameter, return the key/value pairs of the current element in the array, and move the array pointer back 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.

Instance code:
Copy Code code as follows:

<?php
Defines an array of loops
$arr = Array (' website ' => ' http://www.jb51.net ', ' webname ' => ' cloud-dwelling community ')
while (the list ($k, $v) = each ($arr)) {
echo $k. ' => '. $v. ' <br/> ';
}
?> jb51.net

The output results are:
Website=>http://www.jb51.net
webname=>php Programmer

Summary: The above three methods of looping through arrays suggest that you use a foreach statement to iterate through the array, which is more efficient.

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.