$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");
end($my_array);
echo current($my_array); // Fifth One
By using reset() we will take the cursor to first element.
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");
end($my_array);
echo current($my_array); // Fifth One
reset($my_array);
echo current($my_array); //First One
What happens if the pointer is moved beyond the last element ?
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");
while (list ($key, $val) = each ($my_array)) {
echo "$key -> $val <br>";
}
end($my_array);
echo current($my_array); // Fifth One
By using end() we will keep the pointer at the last element of the array.
$my_array=array("First One", "Second One", "Third One", "Fourth One", "Fifth One");
echo current($my_array); // Output : First One
echo next($my_array); // Output : Second One
echo next($my_array); // Output : Third One
echo prev($my_array); // Output : Second One
echo end($my_array); // Output : Fifth One
echo reset($my_array); // Output : First One
$arr = ['apple', 'banana', 'cherry', 'date'];
while ($value = end($arr)) {
echo $value . " ";
array_pop($arr);
}
// Output: date cherry banana apple
$arr = [];
echo end($arr); // Output: false
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.