note 19072 rejected from function.each by didou

From: Date: Mon, 18 Aug 2003 16:00:53 +0000
Subject: note 19072 rejected from function.each by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-54596@lists.php.net to get a copy of this message
Note Submitter: jordan_howe@hotmail.com ---- You must even use reset() even if you use each() in a separate function. For example: function do_stuff($element) { global $arr2; reset ($arr2); while (list($name,$number) = each($arr2)) { if ($number == $element) { echo ("$name is an index in arr2 for this element!\n"); } } } $arr = array(1,5,9,13); $arr2 = array('first'=>1,'second'=>3,'third'=>5,'fourth'=>7); foreach ($arr as $element) { /* this will fail if do_stuff() doesn't call reset() */ do_stuff($element); } This is a nasty bug. You would think that each time you called a function, it would start fresh as if it had never been called before. I think the pointer should automatically be reset upon exiting a function.

« previous php.notes (#54596) next »