PHP each () function
Examples
Returns the current element key names and values, and the internal pointer moves forward:
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
print_r (each($people));
?>
$people = array("Peter", "Joe", "Glenn", "Cleveland");
print_r (each($people));
?>
Running instance »
Definition and Usage
each () function returns the current element's key names and values, and the internal pointer moves forward.
The key element of the name and return to the array with four key elements. Two elements (1 and Value) contains the key, two elements (0 and Key) contains the key name.
Related methods:
- Current () - Returns the value of the current element in the array.
- End () - the internal pointer to the last element in the array, and output.
- Next () - the internal pointer to the next element in the array, and outputs.
- the PREV () - the internal pointer to the last element of the array, and output.
- RESET () - the internal pointer to the first element in the array, and output.
grammar
each( array )
参数 | 描述 |
---|---|
array | 必需。规定要使用的数组。 |
technical details
return value: | Returns the current element key names and values. The key element of the name and return to the array with four key elements. Two elements (1 and Value) contains the key, two elements (0 and Key) contains the key name. If there are no more elements in the array, the function returns FALSE. |
---|---|
PHP version: | 4+ |
More examples
Example 1
Example at the top of the same page, but in this case through the entire cycle of the output array:
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
reset($people);
while (list($key, $val) = each($people))
{
echo "$key => $val<br>";
}
?>
$people = array("Peter", "Joe", "Glenn", "Cleveland");
reset($people);
while (list($key, $val) = each($people))
{
echo "$key => $val<br>";
}
?>
Running instance »
Example 2
All relevant presentation methods:
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>"; // The current element is Peter
echo next($people) . "<br>"; // The next element of Peter is Joe
echo current($people) . "<br>"; // Now the current element is Joe
echo prev($people) . "<br>"; // The previous element of Joe is Peter
echo end($people) . "<br>"; // The last element is Cleveland
echo prev($people) . "<br>"; // The previous element of Cleveland is Glenn
echo current($people) . "<br>"; // Now the current element is Glenn
echo reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, which is Peter
echo next($people) . "<br>"; // The next element of Peter is Joe
print_r (each($people)); // Returns the key and value of the current element (now Joe), and moves the internal pointer forward
?>
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>"; // The current element is Peter
echo next($people) . "<br>"; // The next element of Peter is Joe
echo current($people) . "<br>"; // Now the current element is Joe
echo prev($people) . "<br>"; // The previous element of Joe is Peter
echo end($people) . "<br>"; // The last element is Cleveland
echo prev($people) . "<br>"; // The previous element of Cleveland is Glenn
echo current($people) . "<br>"; // Now the current element is Glenn
echo reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, which is Peter
echo next($people) . "<br>"; // The next element of Peter is Joe
print_r (each($people)); // Returns the key and value of the current element (now Joe), and moves the internal pointer forward
?>
Running instance »
Complete PHP Array Reference