note 28464 deleted from control-structures.foreach by sniper
| From: | sniper@php.net | Date: | Wed, 08 Oct 2003 04:06:24 +0000 |
| Subject: | note 28464 deleted from control-structures.foreach by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58006@lists.php.net to get a copy of this message | ||
Note Submitter: jemore_nospamformeorikillyou@m6net.fr
----
Another "nested foreach" effect :
<?
global $arr;
$arr = array('a', 'b', 'c', 'd');
function f2()
{
global $arr;
foreach ($arr as $k=>$v)
{
echo "$k=>$v<br>\n";
}
}
function f1()
{
global $arr;
foreach ($arr as $k=>$v)
{
f2();
}
}
f1();
?>
a global array is foreach()ed by two inner function.
The internal pointer of the array is reset on each foreach(),
and because there is only one pointer in the global array,
it doesn't behava as expected.
Workaround : use list() and for() construct.