each() 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.
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.
Syntax
each(array) Parameter Description
array Required. Specifies the array to use.
Example 1
Copy code The code is as follows:
$people = array("Peter", "Joe", "Glenn", "Cleveland");
print_r (each($people));
?>
Output:
Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 ) Example 2
each() is often used in combination with list() to iterate over the array. This example is similar to the previous example, but the entire array is output in a loop:
Copy code The code is as follows:
$people = array("Peter", "Joe", "Glenn", "Cleveland");
reset($people);
while (list($key, $val) = each( $people))
{
echo "$key => $val
";
}
?>
Output:
0 => Peter
1 => Joe
2 => Set the original array pointer, so in the above example if we assign $people to another variable inside the loop, it will cause an infinite loop.
http://www.bkjia.com/PHPjc/324470.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324470.htmlTechArticleeach() definition and usage The each() function generates the key name and sum of the elements pointed to by the current internal pointer of the array An array of key values and moves the internal pointer forward. The returned array contains...