Home >Backend Development >PHP Tutorial >php list & each example code_PHP tutorial
PHP list & each example code definition and usage each() function generates an array consisting of the key name and key value of the element pointed to by the current internal pointer of the array, and moves the internal pointer forward
php tutorial list & each example code
Definition and Usage
The each() function generates an array consisting of the key name and key value of the element currently pointed to by the internal pointer of the array, and moves the internal pointer forward.
The returned array includes four elements: key names 0, 1, key and value. Cells 0 and key contain the key names of the array cells, and 1 and value contain the data.
If the internal pointer exceeds the range of the array, this function will return false.
*/
$sports = array(
'football' => 'good',
'swimming' => 'very www.bkjia.com',
'running' => 'not good');
while (list($key,$value) = each($sports))
{
echo $key.": ".$value."
";
}
/*
Definition and Usage
The list() function assigns values to a set of variables using elements in an array.
Note that, like array(), list() is actually a language construct, not a function.
*/
$sports = array(
'football' => 'good',
'swimming' => 'very well',
'running' => 'not www.bkjia.com');
while ($elem = each($sports)) {
echo $elem['key'].": ".$elem['value']."
";
}