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. Link to comment https://forums.phpfreaks.com/topic/201078-why-keymyarray-does-not-return-key-inside-for-each-loop/ 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; } Link to comment https://forums.phpfreaks.com/topic/201078-why-keymyarray-does-not-return-key-inside-for-each-loop/#findComment-1054958 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? Link to comment https://forums.phpfreaks.com/topic/201078-why-keymyarray-does-not-return-key-inside-for-each-loop/#findComment-1054960 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 Link to comment https://forums.phpfreaks.com/topic/201078-why-keymyarray-does-not-return-key-inside-for-each-loop/#findComment-1054963 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? Link to comment https://forums.phpfreaks.com/topic/201078-why-keymyarray-does-not-return-key-inside-for-each-loop/#findComment-1054966 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 ??? Link to comment https://forums.phpfreaks.com/topic/201078-why-keymyarray-does-not-return-key-inside-for-each-loop/#findComment-1054980 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? Link to comment https://forums.phpfreaks.com/topic/201078-why-keymyarray-does-not-return-key-inside-for-each-loop/#findComment-1054981 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 ..... Link to comment https://forums.phpfreaks.com/topic/201078-why-keymyarray-does-not-return-key-inside-for-each-loop/#findComment-1055061 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. Link to comment https://forums.phpfreaks.com/topic/201078-why-keymyarray-does-not-return-key-inside-for-each-loop/#findComment-1055063 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.