Example of a PHP foreach traversing an array

Source: Internet
Author: User
Tags php foreach

A foreach loop is a variant for loop and allows you to traverse the elements in an array. There are two different versions of the

foreach Loop. The syntax for the Foreach loop is as follows:

foreach (array as value)
{
code to is executed;
}

foreach (array as key => value)
{
code to is executed;
}
The following example shows the Foreach loop, which prints the value of the given array:

<?php
$email = Array (' john.smith@example.com ', ' alex@example.com ');
foreach ($email as $value) {
echo "Processing". $value. " <br/> ";
}
?>

PHP executes each element in dollars, in turn the email loop body once, the dollar value is set to the current element. Elements

To deal with its internal order. Loop continues until the last element of the Foreach loop or the given array is approximately

Beam.

<?php
$person = Array (' name ' => ' Andrew ', ' age ' =>, ' address ' => '), Lincoln

St. ');
foreach ($person as $key => $value) {
echo $key. "is". $value. " <br/> ";
}
?>

In this case, the key to each element is placed in the $ key and the corresponding value is $ attached.

In foreach constructs the non-business array itself, but it is a copy of it. In each loop, the value of the variable $ value can be

To operate, but the original value of the array remains unchanged.

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.