国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

php ?foreach traverses associative arrays

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( 要循環(huán)的數(shù)組變量 as [鍵變量 =>] 值變量){
//循環(huán)的結(jié)構(gòu)體
}

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 => 給刪除掉,讀取的時(shí)候,就只讀取值了。做完上面的實(shí)驗(yàn),你可以打開(kāi)下面的代碼再實(shí)驗(yàn)幾次。

/*
foreach($data  as  $value){
       echo  $value . '<br />';
}
*/
?>

Let’s run it and see the results:

QQ截圖20161114131911.png

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 => '中國(guó)',
       100 => '美國(guó)',
       20 => '韓國(guó)',
       300 => '德國(guó)',
   );

//待會(huì)兒可以自己做做實(shí)驗(yàn),循環(huán)遍歷一下下面的這個(gè)數(shù)組
//$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:

QQ截圖20161114131954.png

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(
           '中國(guó)' => 'china',
           '美國(guó)' => 'usa',
           '德國(guó)' => ' Germany',
       ),

       1 => array(
           '湖北' => 'hubei',
           '河北' => 'hebei',
           '山東' => 'shandong',
           '山西' => 'sanxi',
       ),

);

//注:我們?cè)谑褂胒oreach循環(huán)時(shí),第一次循環(huán)將鍵為0和鍵為1的兩個(gè)數(shù)組賦值給一個(gè)變量($value)。然后,再套一個(gè)循環(huán)遍歷這個(gè)$value變量,$value中的值取出來(lái),賦值給$k和$v。

foreach($data as $value){

   //第一次循環(huán)把國(guó)家的數(shù)組賦值給了$value
   //第二次循環(huán)把中國(guó)的省份的數(shù)組又賦值給了$value
   //因此,我在循環(huán)的時(shí)候把$value再遍歷一次

   foreach($value as $k => $v){
           echo $k . '-----' . $v .'<br />';
   }

   //為了看的更清晰,我在中間加上華麗麗的分割線方便你來(lái)分析

   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(
   '教學(xué)部'=>array(
       array('李某','18','人妖'),
       array('高某','20','男'),
       array('張某','21','妖人'),
   ),
   '宣傳部'=>array(
       array('李某','18','人妖'),
       array('高某','20','男'),
       array('張某','21','妖人'),
   ),
   '財(cái)務(wù)部'=>array(
       array('李某','18','人妖'),
       array('高某','20','男'),
       array('張某','21','妖人'),
   ),
);
?>

, the effect is as follows:

QQ截圖20161114132027.png

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.


Continuing Learning
||
<?php $arr=array( '教學(xué)部'=>array( array('李某','18','人妖'), array('高某','20','男'), array('張某','21','妖人'), ), '宣傳部'=>array( array('李某','18','人妖'), array('高某','20','男'), array('張某','21','妖人'), ), '財(cái)務(wù)部'=>array( array('李某','18','人妖'), array('高某','20','男'), array('張某','21','妖人'), ), ); ?>
submitReset Code