In the article, you will learn how to move the internal pointer of an array to the previous element, The prev() function moves the internal pointer to the previous element (if present) and outputs it.
What is the syntax of the prev() function in PHP?
prev(array)
Parameter | Details |
---|---|
array | The array to get the previous element of – Required |
Examples of prev() function
Example 1. Get the current and the previous value of the array.
<?php
$arr= array("Jawad", "Ahmad", "Sumerina");
echo current($arr);
echo prev($arr);
?>
Example 2. In this example, you will find all the methods related to the prev() method category. These methods work with the internal pointer of the arrays.
<?php
$members= array("Jawad", "Ahmad", "Sumerina", "ACS");
// Current element: Jawad
echo current($people) . "<br>";
// Next element of Jawad: Ahmad
echo next($people) . "<br>";
// Now the current element: Ahmad
echo current($people) . "<br>";
// Previous element of Ahmad: Jawad
echo prev($people) . "<br>";
// Last element: ACS
echo end($people) . "<br>";
// Previous element of ACS: Sumerina
echo prev($people) . "<br>";
// Current element: Sumerina
echo current($people) . "<br>";
// Move the internal pointer to the first element of the array:Jawad
echo reset($people) . "<br>";
// Next element of Jawad: Ahmad
echo next($people) . "<br>";
// Get key and value of Ahmad (current element) then moves the internal pointer forward
print_r (each($members));
?>