Forum : How can I reset an array in PHP?
Brief description  about Online courses   join in Online courses
View Arun  Desai 's Profile

How can I reset an array in PHP?

Hi,
How can I reset an array in PHP?
Asked by Arun Desai | Nov 27, 2009 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi arun,

Sometimes if you are moving through the various elements in an array, you will want to return the pointer to the beginning of the array.

This is easily done with the reset function, like this:
reset($array);

This returns the first element and sets it as the current element.

Here is a little example just to make this technique clear:

<?php
$numbers = array("Item 1","Item 2","Item 3");
next($numbers);
$thisvalue = current($numbers); echo "We are now at $thisvalue\n\n";
$first = reset($numbers);
echo "Back to $first";
?>

Printing:

We are now at Item 2

Back to Item 1
Nov 27, 2009