Basic syntax of foreach
We learned to traverse the index array of consecutive subscripts through the content of the previous chapter. However, we found that we cannot traverse associative arrays, nor can we traverse index arrays with discontinuous subscripts.
In fact, when we were learning loops, there was a Boolean loop specially used to loop arrays. The basic syntax of this loop is the basic syntax of foreach.
The syntax format is as follows:
foreach( 要循环的数组变量 as [键变量 =>] 值变量){ //循环的结构体 }
Traverse associative array
This is a fixed usage, put the array to be looped in .
as is a fixed keyword
The key variable after it is optional. Define a variable at will. Every time it loops, the foreach syntax will take out the key and assign it to the key variable.
After The value variable is required. Each time it loops, the value is placed in the value variable.
Let’s use code as an example to enhance our understanding of this syntax.
<?php $data = [ 'fj' => '凤姐', 'fr' => '芙蓉', ]; foreach($data as $key => $value){ echo $key . '-------' . $value . '<br />'; } //如果我们只想读取值的话,就可以把下面的$key => 给删除掉,读取的时候,就只读取值了。做完上面的实验,你可以打开下面的代码再实验几次。 /* foreach($data as $value){ echo $value . '<br />'; } */ ?>
Let’s run it and see the results:
Through the above running results we get the following results:
1. Each time you loop, assign the subscript to the variable $key, and assign the value variable to the variable $value
2. Read the key and value once in the loop. As in the above example, after reading "Sister Feng", read "Furong". After reading to the end, when it is found that there are no array elements that can be read, the loop stops traversing the data.
Note: $key and $value do not have to be the variable names. You can also name it something else, such as $kai => $wen is the same. You need to know which variable the key is assigned to and which other variable the value is assigned to.
Traversing the index array
foreach is quite easy to learn. Therefore, we can traverse the continuous index array through foreach, as in the following example:
<?php $data = array( 0 => '中国', 100 => '美国', 20 => '韩国', 300 => '德国', ); //待会儿可以自己做做实验,循环遍历一下下面的这个数组 //$data = array(1,2,3,4,5,6,7,8,9,10); foreach($data as $k => $v){ echo $k . '------' . $v .'<br />'; } ?>
Run and see the results:
Reason according to the results of foreach and what you just did The result for an associative array is the same.
The difference is the discontinuous index array. Each time an element of the array is read, the subscript of the current loop is assigned to the variable $k, and the value is assigned to the variable $v. The key and value are output for each read and then displayed. The loop moves backward one index at a time. Read to the end and exit execution.
Traversing multi-dimensional arrays
How should we traverse when there is another array in the array? Let’s do an experiment:
<?php $data = array( 0 => array( '中国' => 'china', '美国' => 'usa', '德国' => ' Germany', ), 1 => array( '湖北' => 'hubei', '河北' => 'hebei', '山东' => 'shandong', '山西' => 'sanxi', ), ); //注:我们在使用foreach循环时,第一次循环将键为0和键为1的两个数组赋值给一个变量($value)。然后,再套一个循环遍历这个$value变量,$value中的值取出来,赋值给$k和$v。 foreach($data as $value){ //第一次循环把国家的数组赋值给了$value //第二次循环把中国的省份的数组又赋值给了$value //因此,我在循环的时候把$value再遍历一次 foreach($value as $k => $v){ echo $k . '-----' . $v .'<br />'; } //为了看的更清晰,我在中间加上华丽丽的分割线方便你来分析 echo '----------分割线-----------<br />'; } ?>
The result comes out:
Summary:
In the first loop, the array is assigned to $value , and then use foreach to loop over $value. Give the key in the two-dimensional subarray to $k and assign the value to the variable $v.
The first loop exits the loop of the sub-array, and the subsequent code is executed to display the dividing line.
And so on, the same is true for the second cycle.
Job
Traverse and display the following array:
<?php $arr=array( '教学部'=>array( array('李某','18','人妖'), array('高某','20','男'), array('张某','21','妖人'), ), '宣传部'=>array( array('李某','18','人妖'), array('高某','20','男'), array('张某','21','妖人'), ), '财务部'=>array( array('李某','18','人妖'), array('高某','20','男'), array('张某','21','妖人'), ), ); ?>
, the effect is as follows:
Summary :
1. In the first loop, assign the array to $value, and then use foreach to loop $value. Give the key in the two-dimensional subarray to $k and assign the value to the variable $v.
2. The first loop exits the sub-array loop, and the subsequent code is executed to display the dividing line.
3. And so on, the same is true for the second cycle.