Home > Backend Development > PHP Tutorial > Introduction to the usage of php foreach loop_PHP tutorial

Introduction to the usage of php foreach loop_PHP tutorial

WBOY
Release: 2016-07-13 10:44:33
Original
1120 people have browsed it

In php, the foreach statement is used to loop through the array. Each time the loop is performed, the value of the current array element will be assigned to the value variable (the array pointer will move downward one by one)

Example

The code is as follows Copy code
 代码如下 复制代码

/* php foreach循环简单例子 */
$a = array('a'=>'网上','b'=>'朋友','c'=>'其它',);   
// 这是一个hash数组,我们现在把内容输出   
foreach ($a as $key => $value) {   
    echo $key . '->' . $value . '
';   
}   
?>  
$a = array('a'=>'网上','b'=>'朋友','c'=>'其它',);

/* Simple example of php foreach loop */
$a = array('a'=>'Online','b'=>'Friends','c'=>'Others',); 
// This is a hash array, we now output the content
foreach ($a as $key => $value) {
echo $key . '->' . $value . '
'; } 

?>
$a = array('a'=>'Online','b'=>'Friends','c'=>'Others',);

 代码如下 复制代码

1,"two"=>2,"three"=>3,"four"=>4);
foreach($array1 as $key=>$value)
{echo "KEY:$key value: $value n";}

$array2=array(5,6,7,8);
foreach($array2 as &$value)
{
$value=$value*2;
echo "$valuen";
}

?>

结果是

$array1=array("one"=>1,”two”=>2,”three”=>3,”four”=>4);
foreach($array1 as $key=>$value)
{echo “KEY:$key value: $value n”;}

$array2=array(5,6,7,8);
foreach($array2 as &$value)
{
$value=$value*2;
echo “$valuen”;
}

?>


Start running foreach for the first time, then $key='a';$value='online';
Run the second time after output, then $key='b';$value='friend';

Run the third time after output. At this time $key='c';$value='other';
 代码如下 复制代码
foreach ($array as $key=>$value)
{
echo $key.'=>'$value;
}
At this time, the array has reached the end and exits the loop

Example

The code is as follows Copy code

代码如下 复制代码
foreach ($array as &$row) {
$row = explode(‘/’, $row);
}
foreach ($array as $row) {
//do something
}
1,"two"=>2,"three"=>3,"four"=>4); foreach($array1 as $key=>$value) {echo "KEY:$key value: $value n";} $array2=array(5,6,7,8); foreach($array2 as &$value) { $value=$value*2; echo "$valuen"; } ?> The result is $array1=array("one"=>1,"two"=>2,"three"=>3,"four"=>4); foreach($array1 as $key=>$value) {echo “KEY:$key value: $value n”;} $array2=array(5,6,7,8); foreach($array2 as &$value) { $value=$value*2; echo “$valuen”; } ?>

For ease of understanding, we assume that $array here is a one-dimensional related array, $key is the index of the array, and $value is the value of this index. Their names can be arbitrary, so they are called $key and $value It is for ease of understanding. In order to give you a better understanding of the working process of foreach, let’s create an array: $array = array('first'=>'ibm','second','hp'); Now we simulate the PHP service and use foreach to traverse $array:
The code is as follows Copy code
foreach ($array as $key=> $value) { echo $key.'=>'$value; }
The first loop, $key = 'first', $value = 'ibm', at this time, foreach actually performs an operation on $array that we cannot see Be careful when using references in foreach loop I found a problem that is easy to make mistakes, but I don’t understand the principle but can’t explain it
The code is as follows Copy code
foreach ($array as &$row) { $row = explode(‘/’, $row); } foreach ($array as $row) { //do something }

If you write it like this, there will be a logic error in the second loop. The place where you add do something in the second loop is to output $row. When the loop reaches the last one, the output is the penultimate element, not the last one.

Write like this

The code is as follows
 代码如下 复制代码

foreach ($array as &$row) {
$row = explode(‘/’, $row);
}
unset($row);
foreach ($array as $row) {
//do something
}

或者第一个循环这么写

foreach ($array as $key => $row) {
$array[$key] = explode(‘/’, $row);
}

Copy code
foreach ($array as &$row) {
$row = explode(‘/’, $row);
}
unset($row);
foreach ($array as $row) {
//do something
}

Or write the first loop like this

http://www.bkjia.com/PHPjc/633099.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/633099.htmlTechArticleIn php, the foreach statement is used to loop through the array. Each time the loop is performed, the value of the current array element will be Assign a value to the value variable (the array pointer will move down one by one). For example, the code is...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template