PHP each() Function
In this chapter you will learn:
- Definition for PHP each() Function
- Syntax for PHP each() Function
- Parameter for PHP each() Function
- Related methods for PHP each() Function
- Return value from each() function
- Note for PHP each() Function
- Example - Output current element
- Example - A loop to output the whole array
- Example - A demonstration of all related methods
Definition
The each() function returns the current element key and value, and moves the internal pointer forward.
Syntax
PHP each() Function has the following syntax.
each(array)
Parameter
Parameter | Is Required | Description |
---|---|---|
array | Required. | Specifies the array to use |
Related methods
Method | Description |
---|---|
current() | returns the value of the current element in an array |
end() | moves the pointer to, and outputs, the last element in the array |
next() | moves the pointer to, and outputs, the next element in the array |
prev() | moves the pointer to, and outputs, the previous element in the array |
reset() | moves the pointer to the first element of the array |
Return
each() returns a four - element array rather than a value.
The four-element array that each() returns contains elements with both numeric and string indices, as follows:
Element Index | Element Value |
---|---|
0 | The current element's key |
"key" | The current element's key |
1 | The current element's value |
"value" | The current element's value |
We can use an index of either 0 or "key" to access the current element's key, or an index of 1 or "value" to access its value. For example:
<?PHP//from jav a 2 s .c om
$myBook = array( "title" => "Learn PHP from java2s.com",
"author" => "java2s.com",
"pubYear" => 2000 );
$element = each( $myBook );
echo "Key: " . $element[0] . "\n";
echo "Value: " . $element[1] . "\n";
echo "Key: " . $element["key"] . "\n";
echo "Value: " . $element["value"] . "\n";
?>
The code above generates the following result.
Note
The each()
function does not move the array cursor back to the first element.
It picks up from where the cursor was.
each()
can deal with empty variables and unknown keys.
Example 1
Output current element
<?php
$people = array("A", "B", "C", "D");
print_r (each($people));
?>
The code above generates the following result.
Example 2
A loop to output the whole array:
<?php/*from j a va 2s. co m*/
$people = array("A", "B", "C", "D");
reset($people);
while (list($key, $val) = each($people)){
echo "$key => $val\n";
}
?>
The code above generates the following result.
Example 3
A demonstration of all related methods:
<?php/*j a v a 2 s . c o m*/
$people = array("A", "B", "C", "D");
echo current($people) . "\n"; // The current element is A
echo next($people) . "\n"; // The next element of A is B
echo current($people) . "\n"; // Now the current element is B
echo prev($people) . "\n"; // The previous element of B is A
echo end($people) . "\n"; // The last element is D
echo prev($people) . "\n"; // The previous element of D is C
echo current($people) . "\n"; // Now the current element is C
echo reset($people) . "\n"; // Moves the internal pointer to the first element of the array, which is A
echo next($people) . "\n"; // The next element of A is B
print_r (each($people)); // Returns the key and value of the current element (now B), and moves the internal pointer forward
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP end() Function
- Syntax for PHP end() Function
- Parameter for PHP end() Function
- Related methods for PHP end() Function
- Note for Related methods
- Example - Goes to the end of an array
- Example - A demonstration of all related methods