note 52892 added to control-structures.foreach
| From: | PaulChateau at osu1 dot php dot net | Date: | Tue, 17 May 2005 06:01:19 +0000 |
| Subject: | note 52892 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89514@lists.php.net to get a copy of this message | ||
I had the same problem with foreach() and a recursiv function. If you don't want to spend about
1 or 2 hours to solve this problem, just use for() loops instead of foreach().
Some Example:
$arr[] = array(1,"item1");
$arr[] = array(2,"item2");
$arr[] = array(1,"item3");
//$arr[] = ...
//doesn't work
function foo($x) {
global $arr; // some Array
foreach($arr as $value) {
if($value[0] == $x) {
echo $value[1]."\n";
foo($value[0]);
}
}
}
//just use this
function foo($x) {
global $arr; // some Array
for($i=0; $i < count($arr); $i++) {
if($arr[$i][0] == $x) {
echo $arr[$i][1]."\n";
foo($arr[$i][0]);
}
}
}
Paul
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52892
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52892&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52892&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52892&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52892&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52892&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52892&report=yes
Search -- http://master.php.net/manage/user-notes.php