Php foreach looping array method

Source: Internet
Author: User
Tags explode php foreach php tutorial

The foreach statement is used to traverse arrays cyclically.

For each loop, the value of the current array element is assigned to the value variable (the array pointer is moved one by one), and so on.

Syntax

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

Example
The following example demonstrates a loop that outputs the value of a given array:

<Html>
<Body>

<? Php Tutorial
$ Arr = array ("one", "two", "three ");

Foreach ($ arr as $ value)
{
Echo "value:". $ value. "<br/> ";
}
?>

</Body>
</Html>

The following is a detailed description.

Foreach ($ array as & $ row ){
$ Row = explode ('/', $ row );
}
Foreach ($ array as $ row ){
// Do something
}

In this case, a logic error occurs in the second loop. In the second loop, do something outputs $ row, and the output at the end of the loop is the second-to-last element, instead of the last

Write it like this
Copy the code as follows:

Foreach ($ array as & $ row ){
$ Row = explode ('/', $ row );
}
Unset ($ row );
Foreach ($ array as $ row ){
// Do something
}

Or write in the first loop.
Copy the code as follows:

Foreach ($ array as $ key => $ row ){
$ Array [$ key] = explode ('/', $ row );
}
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.