note 112132 added to function.key
| From: | B.Keil | Date: | Wed, 08 May 2013 12:54:26 +0000 |
| Subject: | note 112132 added to function.key | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194736@lists.php.net to get a copy of this message | ||
A little warning regarding a trap I fell into:
"foreach()" loops have no effect on the output of this situation. The documentation of
"foreach()" says:
"As foreach relies on the internal array pointer changing it within the loop may lead to
unexpected behavior. "
Whatever that is supposed to mean, it does not use the internal array pointer in a way that you
could read the current position with "key()". The following two loops are NOT equivalent:
<?php
$array = array('a', 'b', 'c');
foreach ($array as $value) {
echo key($array)." => $value\n";
}
echo "\n";
foreach ($array as $key => $value) {
echo "$key => $value\n";
}
?>
Output:
1 => a
1 => b
1 => c
0 => a
1 => b
2 => c
----
Server IP: 213.172.110.14
Probable Submitter: 87.153.124.72
----
Manual Page -- http://php.net/manual/en/function.key.php
Edit -- https://master.php.net/note/edit/112132
Del: integrated -- https://master.php.net/note/delete/112132/integrated
Del: useless -- https://master.php.net/note/delete/112132/useless
Del: bad code -- https://master.php.net/note/delete/112132/bad+code
Del: spam -- https://master.php.net/note/delete/112132/spam
Del: non-english -- https://master.php.net/note/delete/112132/non-english
Del: in docs -- https://master.php.net/note/delete/112132/in+docs
Del: other reasons-- https://master.php.net/note/delete/112132
Reject -- https://master.php.net/note/reject/112132
Search -- https://master.php.net/manage/user-notes.php