Phpforeach array traversal example-PHP source code

Source: Internet
Author: User
Ec (2); a foreach loop is a variant For loop that allows you to traverse elements in the array. There are two different versions of foreach loops. The syntax of the foreach loop is as follows: foreach (arrayasvalue) {& nbsp; codetobeexecuted ;}& nbsp; & nbsp; foreach (arrayaskey & gt; value) {script ec (2); script

A foreach loop is a variant For loop that allows you to traverse elements in the array. There are two different versions

Foreach loop. The syntax of the foreach loop is as follows:

Foreach (array as value)
{
Code to be executed;
}

Foreach (array as key => value)
{
Code to be executed;
}
The following example demonstrates a foreach loop that prints the value of a given array:

$ Email = array ('John. smith@example.com ', 'Alex @ example.com ');
Foreach ($ email as $ value ){
Echo "Processing". $ value ."
";
}
?>

PHP executes each element in USD. In turn, the email body is cyclically sent once, and the dollar value is set to the current element. Element

Handle the domestic order. Continues until the last element of the foreach loop or the approx. of the given array is reached.

.

$ Person = array ('name' => 'Andrew ', 'age' => 21, 'address' => '77, Lincoln

St .');
Foreach ($ person as $ key => $ value ){
Echo $ key. "is". $ value ."
";
}
?>

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

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

Operation, 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.