From: Shreyas on
PHP'ers,

I am reading a PHP book which explains foreach and at the end says : *'When
foreach starts walking through an array, it moves the pointer to
the beginning of the array. You don’t need to reset an array before
walking through it with foreach.'*
*
*
*Does this mean - *
*1) Before I navigate the array, foreach will bring the pointer to the
starting key?*
*2) After the first index, it goes to 2nd, 3rd, and nth? *


Regards,
Shreyas
From: Adam Richardson on
On Wed, Jun 9, 2010 at 9:49 PM, Shreyas <shreyasbr(a)gmail.com> wrote:

> PHP'ers,
>
> I am reading a PHP book which explains foreach and at the end says : *'When
> foreach starts walking through an array, it moves the pointer to
> the beginning of the array. You don’t need to reset an array before
> walking through it with foreach.'*
> *
> *
> *Does this mean - *
> *1) Before I navigate the array, foreach will bring the pointer to the
> starting key?*
> *2) After the first index, it goes to 2nd, 3rd, and nth? *
>
>
> Regards,
> Shreyas
>

Number 1.

Adam

--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com
From: Jim Lucas on
Shreyas wrote:
> PHP'ers,
>
> I am reading a PHP book which explains foreach and at the end says : *'When
> foreach starts walking through an array, it moves the pointer to
> the beginning of the array. You don�t need to reset an array before
> walking through it with foreach.'*
> *
> *
> *Does this mean - *
> *1) Before I navigate the array, foreach will bring the pointer to the
> starting key?*
> *2) After the first index, it goes to 2nd, 3rd, and nth? *
>
>
> Regards,
> Shreyas
>

Here is your best reference:

http://php.net/foreach

Look at the two Notes sections on the top of the page.

The first says this:

Note: When foreach first starts executing, the internal array pointer is
automatically reset to the first element of the array. This means that
you do not need to call reset() before a foreach loop.

Basically what you said. But then the second says this

Note: Unless the array is referenced, foreach operates on a copy of the
specified array and not the array itself. foreach has some side effects
on the array pointer. Don't rely on the array pointer during or after
the foreach without resetting it.

--
Jim Lucas

A: Maybe because some people are too annoyed by top-posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
From: Daniel Brown on
On Wed, Jun 9, 2010 at 21:49, Shreyas <shreyasbr(a)gmail.com> wrote:
> PHP'ers,
>
> I am reading a PHP book which explains foreach and at the end says : *'When
> foreach starts walking through an array, it moves the pointer to
> the beginning of the array. You don’t need to reset an array before
> walking through it with foreach.'*
> *
> *
> *Does this mean - *
[snip!]

An easy way to think about it: foreach is cocky and doesn't give a
damn about the rules array functions or placements have set in place.
It'll start from the beginning, and to hell with everyone else.

In other words: foreach will iterate wholly; it will count *for*
*each* key in the loop, not just where another portion of the code
left off.

--
</Daniel P. Brown>
daniel.brown(a)parasane.net || danbrown(a)php.net
http://www.parasane.net/ || http://www.pilotpig.net/
We now offer SAME-DAY SETUP on a new line of servers!
From: tedd on
At 7:19 AM +0530 6/10/10, Shreyas wrote:
>PHP'ers,
>
>I am reading a PHP book which explains foreach and at the end says : *'When
>foreach starts walking through an array, it moves the pointer to
>the beginning of the array. You don't need to reset an array before
>walking through it with foreach.'*
>*
>*
>*Does this mean - *
>*1) Before I navigate the array, foreach will bring the pointer to the
>starting key?*
>*2) After the first index, it goes to 2nd, 3rd, and nth? *
>
>
>Regards,
>Shreyas

Shreyas:

This is one of those questions that you can test very easily, just
initialize an array and try it.

<?php

$test = array(a, b, c, d);
foreach ($test as $value)
{
echo("value = $value <br>");
}

?>

As the references show, there are two versions of the "foreach", the
one above and this:

<?php

$test = array(a, b, c, d);
foreach ($test as $key => $value)
{
echo("$key= $key value=$value <br>");
}

?>

Note that you can pull-out the index (i.e., $key) as well as the
value (i.e., $value) of each index. The "<br>" is only to add a
linefeed in html.

This is a bit easier than using a for() loop.

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
 |  Next  |  Last
Pages: 1 2 3
Prev: Passing large object between methods
Next: Redirect to