PHP array can be treated as a collection of similar data types. PHP Array Iterative functions can be used to access the elements as first element, last element, previous element or next element. The concept of pointers is used here. When an array is created a hidden array pointer stores the address of first element by default. When any of iterative functions is called that action is performed on array and an element is returned.
PHP array iterative functions treat array as a doubly linked list of similar values. Each element has direct access to the next and previous element.
Alert: When an array is created the array pointer is automatically initiated with the address of first element. You can move through the elements by calling relevant function from the below listed functions.
Types of PHP Array Iterative Functions
current()
Current() function returns the value of the element whose address is currently stored in the array pointer.
Syntax:
$varName=current($arrayName);
$varName is the name of the variable that accepts the output of the current function. $arrayName is the name of the array in which you want access the current element.
Alert: The array pointer is not updated after the current function is called.
next ()
next() function first updates the array pointer with the address of the next element. It is relative to the address of the current element. It then returns the value of this element.
Syntax:
$varName=next($arrayName);
$varName is the name of the variable that accepts the output of the next function. $arrayName is the name of the array from which you want to get the next element relative to the current pointer.
Alert: The array pointer is updated after the next function is called.
Alert: If the array pointer reaches the end of array and the next function is called array pointer stays at the last element.
prev ()
prev() function updates the array pointer with the address of previous element. It is relative to address of the current element. It then returns the value of this newly located element.
Syntax:
$varName=prev($arrayName);
$varName is the name of the variable that accepts the output of the prev function. $arrayName is the name of the array from which you want to read the previous element relative to the current pointer.
Alert: The array pointer is updated when the prev function is called.
Alert: If the array pointer reaches the beginning of array and prev function is called, array pointer stays at the first element of the array.
reset ()
reset() function updates the array pointer with the address of the first element of the array. It then returns the value of the first element.
Syntax:
$varName=reset($arrayName);
$varName is the name of the variable that accepts the output of reset function. $arrayName is the name of the array in which you want to reach to the first element.
Alert: The array pointer is updated when the reset function is called.
end ()
end() function updates the array pointer with the address of the last element of the array. It then returns the value of the last element.
Syntax:
$varName=last($arrayName);
$varName is the name of the variable that accepts the output of end function. $arrayName is the array in which you want to move the array pointer to last element.
Alert: The array pointer is updated when the end function is called.
Example Code
<?php $arrVals=array(30,23,59,93,50,11); print_r($arrVals); echo "<h3> PHP array Iteration functions</h3>"; echo "<br>current->".current($arrVals)."<br>"; echo "next->".next($arrVals)."<br>"; echo "next->".next($arrVals)."<br>"; echo "next->".next($arrVals)."<br>"; echo "prev->".prev($arrVals)."<br>"; echo "end->".end($arrVals)."<br>"; echo "reset->".reset($arrVals)."<br>"; ?>
Output
PHP Array Iterative functions are very helpful to access the array elements as though you can move pointer among them. You can read more about these functions at the official site PHP.NET