What are next() and prev() Functions?
To find out the index value of a new element added to an array, you use the next() and prev() functions. These functions enable you to navigate through arrays by moving the pointer to the next or previous element in the array. They both take the name of the array to navigate through as an argument. For example, in the preceding array creation list:
$director [4]=”Orson Welles”;
$director [1]=”Carol Reed”; $director[93]=”Fritz Lang”;
$director [24]=”Jacques Tourneur”;
$director[]=”Alfred Hitchcock”;
You call the next() function before checking the current index, and the contents of the current element
next ($director);
$current_index_value = key ($director);
echo ($current_index_value);
The value displayed is 1, and the current () function returns the name Carol Reed.
If you call next() three more times:
next ($director);
next ($director);
next ($director);
next ($director);
$current_index_value = key ($director);
echo ($current_index_value);
The value 94 is displayed. If you now call the current () function:
$current_contents = current ($director);
echo ($current_contents);
The browser displays Alfred Hitchcock.
The prev() function is used in a similar way. Take the preceding example and add one occurrence of prev () after the four of next():
next ($director);
next ($director);
next ($director);
next ($director);
prev ($director);
$current_index_value = key ($director);
echo ($current_index_value);