List (), each (), reset () function application, eachreset in PHP
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 ()Assign values to a group of variables in one step.
Note: list () can only be used as an array of numeric indexes and assumes that the numeric index starts from 0.
<? Php $ arr = array (1, 2, 3); list ($ a, $ B, $ c) = $ arr; echo "\ $ a is $, <br/> \ $ B is $ B, <br/> \ $ c is $ c. <br/> ";?>
Shown:
$ A is 1,
$ B is 2,
$ C is 3.
Note: 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 )
ReturnArrayThe key/value pair of the current pointer position in the array and move the array Pointer Forward.
The key-value pair is returned as an array of four units. The key value is0,1,KeyAndValue. Unit0AndKeyKey name that contains an array unit,1AndValueContains data.
If the internal pointer goes beyond the end of the arrayEach ()ReturnFALSE. Why does the each function have four subscripts?
In fact, the each function obtains these four Subscripts for our convenience. We can use 0, 1 as the index, or key and value as the index.
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 combine the each function with the list function.Traverse Arrays, For example:
<?php$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');reset($fruit);while (list($key, $val) = each($fruit)) { echo "$key => $val\n";}?>
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:
In executionEach ()Then, 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 () willArrayReturns the value of the first array unit. If the array is emptyFALSE.
You can compare the above example 2 with the following example to understand it easily...
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 firstICount// Compare it with this line in example 2.