Tuesday, July 14, 2009

How can i reset an array in PHP?

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

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:

$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";
?>

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home