PHP each() Function
Example
Returns the key name and key value of the current element and moves the internal pointer forward:
<?php $people = array("Bill", "Steve", "Mark", "David"); print_r (each($people)); ?>
Definition and Usage
The each() function returns the key name and key value of the current element and moves the internal pointer forward.
The key name and key value of the element will be returned in an array with four elements. Two elements (1 and Value) contain the key value, and two elements (0 and Key) contain the key name.
Related Methods:
- current() - Returns the value of the current element in the array
- end() - Points the internal pointer to the last element in the array and outputs
- next() - Points the internal pointer to the next element in the array and outputs
- prev() - Points the internal pointer to the previous element in the array and outputs
- reset() - Points the internal pointer to the first element in the array and outputs
Syntax
each(array)
Parameter | Description |
---|---|
array | Required. Specifies the array to be used. |
Description
The each() function generates an array consisting of the key name and key value of the element pointed to by the current internal pointer and moves the internal pointer forward.
The array returned includes four elements: the key name is 0, 1, key, and value. Unit 0 and key contain the key name of the array element, and 1 and value contain the data.
If the internal pointer goes beyond the array range, this function will return FALSE.
Technical Details
Return Value: |
Returns the key name and key value of the current element. The key name and key value of the element are returned in an array with four elements. Two elements (1 and Value) contain the key-value, and two elements (0 and Key) contain the key names. If there are no more array elements, the function returns FALSE. |
PHP Version: | 4+ |
More Examples
Example 1
The instance at the top of the page is the same, but in this example, the entire array is outputted in a loop:
<?php $people = array("Bill", "Steve", "Mark", "David"); reset($people); while (list($key, $val) = each($people)) { echo "$key => $val<br>"; } ?>
Example 2
Demonstrate all related methods:
<?php $people = array("Bill", "Steve", "Mark", "David"); echo current($people) . "<br>"; // The current element is Bill echo next($people) . "<br>"; // Bill's next element is Steve echo current($people) . "<br>"; // The current element is now Steve echo prev($people) . "<br>"; // Bill is the previous element of Steve echo end($people) . "<br>"; // The last element is David echo prev($people) . "<br>"; // The element before David is Mark echo current($people) . "<br>"; // The current element is Mark echo reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, which is Bill echo next($people) . "<br>"; // Bill's next element is Steve print_r (each($people); // Returns the key name and value of the current element (currently Steve) and moves the internal pointer forward ?>