Array cursor.
Posted: Tue Aug 07, 2007 8:23 pm
What does this page mean by "Advances the array cursor"? What's the array cursor?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
The array cursor is an internal pointer that points to an element in an array. The current function will fetch the "current" element in an array that the array cursor points to. This pointer is stored as part of the array internally within PHP. It is useful for looping through an array. Before PHP had the foreach a common way to look through an array was by using while like soo ...JellyFish wrote:What does this page mean by "Advances the array cursor"? What's the array cursor?
Code: Select all
while (list($key, $val) = each($fruit)) {
echo "$key => $val ";
}Code: Select all
$v1 = each($array);
$v2 = each($array);
Code: Select all
$x = array(1, 2, 3);
list(, $value) = each($x);
echo $value; // would echo 1
list(, $value) = each($x);
echo $value; // would echo 2
reset($x);
list(, $value) = each($x);
echo $value; // would echo 1What do you mean?JellyFish wrote:But does each array get it's own array cursor?
Yes.JellyFish wrote:But does each array get it's own array cursor?
Code: Select all
while (each($array))
{
}
//then use each for for the same array
echo each($array);
Code: Select all
<?php
echo "<pre>\n";
$arr = array('a', 'b', 'c');
while ( $e=each($arr) ) {
var_dump($e);
}
echo "1---\n";
var_dump(each($arr));
echo "2---\n";
$arr[] = 'xyz';
var_dump(each($arr));
echo "</pre>\n";