|
| ITERATING MANUALLY THROUGH AN ARRAY
Iteration refers to a process of walking or traversing through elements of an array and doing something with the values.
Iteration can be done manually by using a pointer to move through an array from one value to another.
There are a few manual functions you can use for this process. These are:
current($array):
Refers to the value the pointer is currently on. By default, at the beginning, the pointer is on the first value.
next($array):
Moves the pointer to the next value to the right of current value.
previous($array):
Moves the pointer to the previous value ie. to the left of current value.
end($array):
Moves the pointer to the last value of the array.
reset($array):
Moves the pointer to the first value of the array.
Example:
$colors=array('blue', 'red', 'green', 'white', 'purple');
$value=current($colors);
echo $value; // outputs blue
$value=next($colors);
echo $value; // outputs red
$value=previous($colors);
echo $value; // outputs blue
$value=end($colors);
echo $value; // outputs purple
$value=reset($colors);
echo $value; // outputs blue
See also:
|
|