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:
##Conclusion: 1.list The first and second places for variables are left blank, I only wrote $three. 2. According to the one-to-one correspondence principle, there are no variables that can correspond to Zhang San and Li Si. 3. So only Wang Wu has variable correspondence Please strictly remember: the one-to-one correspondence principle of the index array. The first variable in the list corresponds to the array element with subscript 0, and the subscript 1 corresponds to the second array element in the list. Guess what the result below is? Why?<?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: Summary: 1. Because of the one-to-one correspondence principle, $one cannot be found For the array element marked with 0, $two cannot find the array element with the subscript 1, and only $three finds the array element with the subscript 2 3. In list($one, $two, $ three), I only wrote three variables. The correspondence is completed, and there is no need to correspond to subsequent variables. Discard Li Si and Wang Wu.
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【执行到了最后一个元素了】 $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:
##Summary: 1. Read once and move backward once [You can imagine that there is a record arrow in Move], disassemble each element into a new array 2. At the end of reading, there are no operable elements, so false is returned.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:The first variable in the list will find the element with index 0 below to assign a value, and the second variable will find the element with index subscript 1 to assign a value to the variable. Let’s take a look at the following example:<?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: