Analysis of each and list usage in PHP and phpeachlist usage. PHP each and list usage analysis, phpeachlist usage This article analyzes the each and list usage in PHP. I would like to share it with you for your reference. for details, refer to the usage of each and list in APIarrayea PHP and the usage of phpeachlist.
This article analyzes the usage of each and list in PHP. We will share this with you for your reference. The details are as follows:
1. each usage
First look at the API
Array each (array & $ array)
This is described in the api: each-returns the current key/value pair in the array and moves the array pointer one step forward.
Let's take a look at the returned array first?
<? Php $ arr = array ('you', 'Ruby', 'an', 'haok', 'you', 'yes', 'Qing', 'day '); print_r (each ($ arr); echo'
';/* Returns Array ([1] => you [value] => you [0] => 0 [key] => 0) array ([1] => if [value] => if [0] => 1 [key] => 1) * // execute the same piece of code, from 'you' to 'ru', it indicates that each is executed every time, the cursor moves one step toward the end of the array. // 0 and Key are stored in the Key. // 1 and value are stored in the value. // therefore, each can traverse the array to obtain the current Key and value, and move one cursor to the end of each execution. // therefore, you can use each to write reset ($ arr); for (; $ tmp = each ($ arr );) {echo $ tmp [0], '~ ', $ Tmp [1],'
';}/* Returns 0 ~ You 1 ~ If 2 ~ Security 3 ~ Good 4 ~ 5 ~ Is 6 ~ Clear 7 ~ Day */?>
2. list usage
First, let's look at what the api says.
Like array (), this is not a real function, but a language structure. List () assigns values to a group of variables with one-step operations.
Let's look at an example:
<? Php list ($ a, $ B) = array (10, 20); echo $ ,'~ ', $ B ,'
'; // Return 10 ~ 20?>
You can assign values to a group of variables.
Let's look at another example:
<? Php list ($ a, $ B, $ c) = array (2 => 10, 3 => 20, 4 => 30, 1 => 40); echo $ ,'~ ', $ B ,'~ ', $ C ,'
'; // Return notice ~ 40 ~ 20 // when $ a is executed, a notice is returned to me, indicating that the array does not have a 0 key?>
According to the general idea, 10 ~ will be returned ~ 20 ~ 40
Why does this notice ~ be returned ~ 40 ~ 20?
A: This involves the list operation mechanism. the list value is assigned in this way.
First, do not care about the array on the right. check the variables in the List. The value from left to right should be $ a = arr [0] $ B = arr [1] $ c = arr [3].
Then, assign values from right to left. The order of values is $ c = arr [3] $ B = arr [1] $ a = arr [0].
So $ c = 20 $ B = 40 because there is no arr [0], $ a gives a warning.
3. use each and list to traverse arrays
<? Php $ arr = array ('you', 'Ruby', 'an', 'haok', 'you', 'yes', 'Qing', 'day '); for (; list ($ k, $ v) = each ($ arr);) {echo $ k ,'~ ', $ V ,'
';}/* Return: 0 ~ You 1 ~ If 2 ~ Security 3 ~ Good 4 ~ 5 ~ Is 6 ~ Clear 7 ~ Day */?>
I hope this article will help you with PHP programming.
Articles you may be interested in:
- PHP looping through the three methods of array list (), each () and while Summary
- PHP loop statement notes (foreach, list)
- PHP array traversal method (foreach, list, each)
- Analysis of list () function usage examples in PHP
- In-depth explanation of php list () functions
Examples in this article analyze the usage of each and list in PHP. I would like to share with you for your reference. The details are as follows: 1. the usage of each should first look at API array ea...