How to move the internal pointer of an array in PHP?

Every PHP array has an internal pointer that points to one element. You can move it with these functions:

FunctionWhat it does
current()Returns the element where the pointer is now
key()Returns the key of the current element
next()Moves the pointer one step forward
prev()Moves the pointer one step back
reset()Moves the pointer to the first element
end()Moves the pointer to the last element
<?php
$fruits = ["apple", "banana", "cherry"];

echo current($fruits) . "<br>";
echo next($fruits) . "<br>";
echo next($fruits) . "<br>";
echo prev($fruits) . "<br>";
echo reset($fruits) . "<br>";
echo end($fruits);
?>

Output:

Tutorials Class - Output Window

apple
banana
cherry
banana
apple
cherry

When the pointer goes outside the array, next() and prev() return false.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.