lostnucleus Posted May 8, 2010 Share Posted May 8, 2010 $data = array('cat','bat','rat','net'); foreach($data as $value) { echo key($data); } The output of above code is "111" ??? why it is not 0 , 1 ,2 ,3 ?? Thx in Advance. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 8, 2010 Share Posted May 8, 2010 Well that's interesting. But why not just use: <?php foreach( $data as $key => $value ) { echo $key; } Quote Link to comment Share on other sites More sharing options...
ignace Posted May 8, 2010 Share Posted May 8, 2010 Well that's interesting. No, actually not. key() does not advance the internal array pointer. And the 111 is due to his code, not the above example. Which outputs 000 $data = array('cat','bat','rat','net'); echo key($data), '<br/>'; foreach($data as $key => $value) { echo 'key(): ', key($data), ' $key: ', $key, '<br/>'; } Outputs: 0<br/> key(): 1 $key: 0<br/> key(): 1 $key: 1<br/> key(): 1 $key: 2<br/> key(): 1 $key: 3<br/> foreach() advances the array pointer by 1? Quote Link to comment Share on other sites More sharing options...
Kryptix Posted May 8, 2010 Share Posted May 8, 2010 <?php $data = array("cat", "bat", "rat", "net"); foreach($data as $value) { echo $value; } ?> Output: catbatratnet Quote Link to comment Share on other sites More sharing options...
ignace Posted May 8, 2010 Share Posted May 8, 2010 On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one... ...When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array... ...foreach operates on a copy of the specified array and not the array itself Then why is the original array internal pointer advanced by one? Quote Link to comment Share on other sites More sharing options...
lostnucleus Posted May 8, 2010 Author Share Posted May 8, 2010 On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one... ...When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array... ...foreach operates on a copy of the specified array and not the array itself Then why is the original array internal pointer advanced by one? That Q is killing me , why this is happening ??? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 8, 2010 Share Posted May 8, 2010 Possibly a bug or constraint in how arrays (or foreach) are implemented? Quote Link to comment Share on other sites More sharing options...
lostnucleus Posted May 8, 2010 Author Share Posted May 8, 2010 Possibly a bug or constraint in how arrays (or foreach) are implemented? hmm ... well then sm1 needs to report this bug on php.net ..... Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 8, 2010 Share Posted May 8, 2010 The discrete array functions that modify or access an array pointer - end(), key(), each(), prev(), reset(), next(), ... cannot be used inside of a foreach() loop to access the same array that is being iterated over. There is no need to do so or you should not be using a foreach() loop in the first place. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.