PHP list, each function traverses the array
Let’s study two more interesting array functions. These two functions are not difficult to learn. However, some students will encounter a little difficulty. The difficulty lies in finding the operating characteristics of the list function and each function.
list function
Let’s talk about the list function first:
list ( mixed $variable 1 [, mixed $variable n ] )
Its function: the index array subscript 0 corresponds to variable 1, the subscript 1 corresponds to variable 2, and so on.
Let’s take a look at the experiment:
<?php list($one , $two , $three) = array('張三' ,'李四' ,'王五'); //再次聲明:單引號不結釋變量,所以輸出的是字符串$one echo '$one----'.$one.'<br />'; echo '$two----'.$two.'<br />'; echo '$three----'.$three.'<br />'; ?>
Let’s take a look at the experimental results:
The analysis results are as shown in the figure:
Conclusion:
1. Assign the value of Zhang San with the subscript 0 to $one
2. Assign the value of Zhang San with the subscript 1 Li Si is assigned to $two
3. Wang Wu with subscript 2 is assigned to $three
Therefore, we know that the function of the list is from left to right, one by one Corresponds to the subscript value of the index array starting from 0. Another usage of
list:
<?php list( , , $three) = array('張三' ,'李四' ,'王五'); echo '$one----'.$one.'<br />'; echo '$two----'.$two.'<br />'; echo '$three----'.$three.'<br />'; ?>
Running result:
<?php list($one, $two, $three) = array(2 => '張三', '李四', '王五'); echo '$one----' . $one . '<br />'; echo '$two----' . $two . '<br />'; echo '$three----' . $three . '<br />'; ?>The running results are as follows:
each function
The regularity of each function is more distinctive and interesting.array each ( array &$array )
Function: Pass in an array. It will split one of the elements into a new array. Do this one element at a time. Move once and operate the next array element in the same way. Execute to the end and return false.
Let’s first take a look at how each operates on array elements.
<?php //定義一個變量叫$kongjie(空姐) $kongjie=[ 'gao'=>'穿黑衣服的', 'shou'=>'退特別長特別細', 'mei'=>'好白', 'pl'=>'五官端正', 'type'=>'那就是女神', '我是吊絲不敢跟女神搭訕' ]; //第一次each $data = each($kongjie); echo '<pre>'; var_dump($data); echo '</pre>'; ?>
Let’s take a look at the results of the first execution of each:
Summary:
1. Read the first in $kongjie elements, decomposing the first element ('gao'=>'the one wearing black clothes').
1After decomposition, the first element becomes a new array.
2In the new array, put the original value (the one in black clothes) in the index subscript 1, and at the same time put it in the associated subscript value.
3In the new array, put the original key (gao) into the associated subscript key and into the index subscript 0.
We use pictures to represent:
This way we can understand it at once.
Next let’s talk about another feature of each. Read once and move one element backward.
<?php //定義一個變量叫$kongjie(空姐) $kongjie=[ 'gao'=>'穿黑衣服的', 'shou'=>'退特別長特別細', 'mei'=>'好白', ]; //第一次each $data = each($kongjie); echo '<pre>'; var_dump($data); echo '</pre>'; echo '-----華麗麗分割線------<br />'; //第2次each $data = each($kongjie); echo '<pre>'; var_dump($data); echo '</pre>'; echo '-----華麗麗分割線------<br />'; //第3次each【執(zhí)行到了最后一個元素了】 $data = each($kongjie); echo '<pre>'; var_dump($data); echo '</pre>'; echo '-----華麗麗分割線------<br />'; //第4次【此時,后面已沒有可操作的元素了,看返回什么】 $data = each($kongjie); echo '<pre>'; var_dump($data); echo '</pre>'; echo '-----華麗麗分割線------<br />'; ?>
Execution results:
List and each cooperate
We know the characteristics of list and the characteristics of each. Can list be combined with each to complete some work?list($key,$value) = each($array);
Let’s look at the picture mentioned before:<?php //定義一個變量叫$kongjie(空姐) $kongjie=[ 'gao'=>'穿黑衣服的', 'shou'=>'腿特別長特別細', 'mei'=>'好白', ]; list($key,$value) = each($kongjie); echo $key. '-----' .$value .'<br />'; ?>The running results are as follows:
##Summary:
1.each Split the variable into 4 elements
2. List assigns 0 =>gao to the variable $key
3.list assigns 1 => wearing black clothes to variable $value
Each will return false at the end, so I can use a Boolean loop while to complete the array loop.
Just change the above code to achieve the following effect:
<?php //定義一個變量叫$kongjie(空姐) $kongjie=[ 'gao'=>'穿黑衣服的', 'shou'=>'退特別長特別細', 'mei'=>'好白', ]; while(list($key,$value) = each($kongjie)){ echo $key. '-----' .$value .'<br />'; } ?>
Execution demonstration:
Summary:
1. Loop once, execute each once, execute the code, and then move one element backward
2. Return to fasle at the end of the execution, so execution stops.
3. The same effect as foreach can be achieved by combining each and list.
Assignment:
Use list and each to 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: