1. list function
The list function assigns values in the array to some variables. The syntax is as follows:
Void list (mixed $ varname, mixed $ varname ...)
Like array (), this is not a real function, but a language structure. List () assigns values to a group of variables with one-step operations.
Note: list () can only be used as an array of numeric indexes and assumes that the numeric index starts from 0.
Example 1. list ()
Code:
The code is as follows: |
Copy code |
<? Php $ Arr = array (1, 2, 3 ); List ($ a, $ B, $ c) = $ arr; Echo "$ a is $ a, <br/> $ B is $ B, <br/> $ c is $ c. <br/> "; ?> Shown: $ A is 1, $ B is 2, $ C is 3. |
Note that the subscript of the list function must be a number and start from 0.
2. each and reset functions
The each function returns the current key/value pair in the array and moves the array pointer one step forward. Note that it is a pair, which is described in detail below.
Syntax of this function:
Array each (array & $ array)
Returns the key/value pair of the current pointer position in the array and moves the array pointer forward. The key-value pair is returned as an array of four units.
The values are 0, 1, key, and value. Unit 0 and key contain the key name of the array unit, and 1 and value contain data. If
If the pointer crosses the end of the array, each () returns FALSE. Why are there four tables in the each function? In fact, the each letter
We can use 0, 1 as the index, or key and value as the index.
Example 2: each
Code:
The code is as follows: |
Copy code |
<? Php $ Arr = array ("I am the first value", "I am the second value", "I am the third value "); Echo "when we use 0, 1 as the index: <br/> "; $ A = each ($ arr ); Echo "my position in the $ arr array is:". $ a ['0']; Echo "<br/> "; Echo "my value in the $ arr array is:". $ a ['1']; Echo "<br/> "; Echo "when key and value are used as indexes: <br/> "; $ B = each ($ arr ); Echo "my position in the $ arr array is:". $ B ['key']; Echo "<br/> "; Echo "my value in the $ arr array is:". $ B ['value']; ?> |
Shown:
When we use 0, 1 as the index:
My position in the $ arr array is: 0
My value in the $ arr array is: I am the first value.
When key and value are used as indexes:
My position in the $ arr array is: 1
My value in the $ arr array is: I am the second value.
You can also use the each function and the list function to traverse the array, as shown in the following example:
Example 3:
The code is as follows: |
Copy code |
<? Php $ Fruit = array ('a' => 'apple', 'B' => 'bana', 'C' => 'Cranberry '); Reset ($ fruit ); While (list ($ key, $ val) = each ($ fruit )){ Echo "$ key => $ valn "; } ?>
|
Shown:
A => apple B => banana c => cranberry
Note that the subscript of the list function must be a number and start from 0.
Description of the reset function:
After executing each (), the array pointer will stay in the next unit of the array or stay in the last unit when it comes to the end of the array. If you want to use each to traverse the array, you must use reset (). If this is the first time you traverse the array, you do not need to use it.
For details, refer to the reset function:
Reset is to point the internal pointer of the array to the first unit. Syntax:
Mixed reset (array & $ array)
Reset () returns the internal pointer of array to the first unit and the value of the first array unit. If the array is null, FALSE is returned.
You can compare the above example 2 with the following example to understand it easily... ,
Code:
The code is as follows: |
Copy code |
<? Php $ Arr = array ("I am the first value", "I am the second value", "I am the third value "); Echo "when we use 0, 1 as the index: <br/> "; $ A = each ($ arr ); Echo "my position in the $ arr array is:". $ a ['0']; Echo "<br/> "; Echo "my value in the $ arr array is:". $ a ['1']; Echo "<br/> "; Echo "when key and value are used as indexes: <br/> "; Reset ($ arr ); $ B = each ($ arr ); Echo "my position in the $ arr array is:". $ B ['key']; Echo "<br/> "; Echo "my value in the $ arr array is:". $ B ['value']; ?> |
Shown:
When we use 0, 1 as the index:
My position in the $ arr array is: 0
My value in the $ arr array is: I am the first value.
When key and value are used as indexes:
My position in the $ arr array is: 0
My value in the $ arr array is: I am the first value. // note that this line in Example 2 is displayed.