Output the current element and the value of the next element in an array:
<?php$people = array("Peter", "Joe", "Glenn", "Cleveland");echo current($people) . "<br>";echo next($people);?>The next() function points the internal pointer to the next element in the array and outputs it.
Related methods:
prev() - Sets the internal pointer to the previous element in the array and outputs it.
current() - Returns the value of the current element in the array.
end() - Sets the internal pointer to the last element in the array and outputs it.
reset() - Sets the internal pointer to the first element in the array and outputs it.
each() - Returns the key name and value of the current element and moves the internal pointer forward.
next( array )
parameter | describe |
---|---|
array | Required. Specifies the array to use. |
Return value: | Returns the value of the next element in the array if successful, or FALSE if there are no more array elements. |
---|---|
PHP version: | 4+ |
Demonstration of all relevant methods:
<?php$people = array("Peter", "Joe", "Glenn", "Cleveland");echo current($people) . "<br>"; // The current element is Peterecho next($people) . "<br>"; // The next element of Peter is Joeecho current($people) . "<br>"; // Now the current element is Joeecho prev($people) . "<br>"; // The previous element of Joe is Peterecho end($people) . "<br>"; // The last element is Clevelandecho prev($people) . "<br>"; // The previous element of Cleveland is Glennecho current($people) . "< br>"; // Now the current element is Glennecho reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, which is Peterecho next($people) . "<br >"; // The next element of Peter is Joeprint_r (each($people)); // Returns the key and value of the current element (now Joe), and moves the internal pointer forward?>