Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,679 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,188 people online right now. Registration is fast and FREE... Join Now!




reading from arrays multiple times

 
Reply to this topicStart new topic

reading from arrays multiple times

Study_Girl
post 4 Jul, 2007 - 12:34 AM
Post #1


New D.I.C Head

*
Joined: 27 Jun, 2007
Posts: 13


My Contributions


Hi,

Was just playing around with arrays and watching the output as I use different methods to access them. When I use this code:

CODE

$prices = array('Tyres'=>100, 'Oil'=>10, 'SparkPlugs'=>4);
foreach ($prices as $key => $temp)
     echo $key.'=>'.$temp.'<br />';


it produces this (expected) output
QUOTE

Tyres=>100
Oil=>10
SparkPlugs=>4


when I use this code:

CODE

$prices = array('Tyres'=>100, 'Oil'=>10, 'SparkPlugs'=>4);
while ($element = each($prices))
{
     echo $element['key'];
     echo ' - ';
     echo $element['value'];
     echo '<br />';
}


it produces this (expected) output:
QUOTE

Tyres - 100
Oil - 10
SparkPlugs - 4


but when I have both the code blocks on the one page, and only initialise the array once, it only produces the first output not both... In order to get both outputs I have have line one of both code blocks twice... is there a reason for this?

Cheers

This post has been edited by Study_Girl: 4 Jul, 2007 - 12:38 AM
User is offlineProfile CardPM

Go to the top of the page

Styx
post 4 Jul, 2007 - 01:50 AM
Post #2


D.I.C Head

Group Icon
Joined: 4 Mar, 2007
Posts: 192



Dream Kudos: 225
My Contributions


With foreach, the internal array pointer is advanced by one. By the time it ends the loop, the pointer is set to one beyond the total number of array contents, or null. You can see this by doing var_dump(key($prices)) after the loop.

When the while loop begins, it starts at the current internal array pointer, whatever that may be. But if the pointer is past the end of the array, it'll return false, and the while loop quits before it began.

To reset the internal array pointer, you just put reset($prices) before the while loop. Foreach automatically does this for you.

http://www.php.net/each
http://www.php.net/foreach
http://www.php.net/reset
User is offlineProfile CardPM

Go to the top of the page

Study_Girl
post 4 Jul, 2007 - 02:40 AM
Post #3


New D.I.C Head

*
Joined: 27 Jun, 2007
Posts: 13


My Contributions


QUOTE(Styx @ 4 Jul, 2007 - 02:50 AM) *

...
To reset the internal array pointer, you just put reset($prices) before the while loop. Foreach automatically does this for you.

http://www.php.net/each
http://www.php.net/foreach
http://www.php.net/reset


biggrin.gif Thank you very much!!!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:34AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month