Array cursor.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Array cursor.

Post by JellyFish »

What does this page mean by "Advances the array cursor"? What's the array cursor?
ev0l
Forum Commoner
Posts: 56
Joined: Thu Jun 21, 2007 1:50 pm

Re: Array cursor.

Post by ev0l »

JellyFish wrote:What does this page mean by "Advances the array cursor"? What's the array cursor?
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 ...

Code: Select all

while (list($key, $val) = each($fruit)) {
    echo "$key => $val ";
}


It is very similar to C's concept ofpointer arithmetic.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

So your array has 5 elements. The internal pointer is pointed at the first one (0). You pull one out using each, now it's pointing at the second element (1). Element 0 still exists. You can use the PHP function reset to reset the cursor. I call it a pointer though. Without looking I don't know what the correct term is. It sounds like both are used interchangeably.

Anyway, this allows you to iterate through an array without using a function such as foreach.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

So basically, if I write:

Code: Select all

$v1 = each($array);
$v2 = each($array);
$v1 would equal to the second index in $array?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Like this..

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 1
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

K cool. Now I understand.

But does each array get it's own array cursor?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

JellyFish wrote:But does each array get it's own array cursor?
What do you mean?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

JellyFish wrote:But does each array get it's own array cursor?
Yes.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Okay, well I have another question.

If I looped through the array with:

Code: Select all

while (each($array))
{

}
//then use each for for the same array
echo each($array);
which index in $array would the last statement output? In other words where would the array cursor be?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Why not try it?

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";
Post Reply