PHP reset() Function
In this chapter you will learn:
- Definition for PHP reset() Function
- Syntax for PHP reset() Function
- Parameter for PHP reset() Function
- Return from PHP reset() Function
- Related methods for PHP reset() Function
- Example - Using current and next and reset
- Example - A demonstration of all related methods
Definition
An array cursor is a pointer pointing to the next array element.
The reset()
function rewinds its parameter's cursor to the first element,
then return the value of that element.
Syntax
PHP reset() Function has the following syntax.
reset(array)
Parameter
Parameter | Is Required | Description |
---|---|---|
array | Required. | Specifies the array to use |
Return
If cannot return a value, it will return false.
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 |
each() | returns the current element key and value, and moves the pointer forward |
Example 1
Output the value of the current and next element in an array, then reset the array's internal pointer to the first element in the array:
<?php/*from jav a 2s .c o m*/
$people = array("A", "B", "C", "D");
echo current($people) . "\n";
echo next($people) . "\n";
echo reset($people);
?>
The code above generates the following result.
Example 2
A demonstration of all related methods:
<?php//j av a2s . 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 rsort() Function
- Syntax for PHP rsort() Function
- Parameter for PHP rsort() Function
- Note for PHP rsort() Function
- Example - PHP rsort() Function
- Example - Sort the elements of the $numbers array in descending numerical order
- Example - Compare the items numerically and sort the elements of the $cars array in descending order
Home » PHP Tutorial » PHP Array Functions