Bug #16071: foreach return bogus data on 1 row multi-dim array

From: Date: Thu, 14 Mar 2002 15:10:07 +0000
Subject: Bug #16071: foreach return bogus data on 1 row multi-dim array
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-2493@lists.php.net to get a copy of this message
From:             ned@wgtech.com
Operating system: Win 2000
PHP version:      4.1.0
PHP Bug Type:     Arrays related
Bug description:  foreach return bogus data on 1 row multi-dim array

I have code that traps all errors in a multidimentional array. The array is
structured so each row has a numerical key (ie $errors[] =
array('err_code'=>12, 'err_file'=>'happy.php');). You get the
idea. For
error handling I have a function within a class that will accept an error
array and do output based on it. The function is as follows:

function pass_err_row($errors, $default_val=NULL, $override_val=NULL) { //
Throw errors based on err_file or default
	if (is_null($default_val))
{$default_val=PATH_TO_ROOT.LOC_ERROR."DEFAULT/errors.php";}
	if (!is_null($override_val)) {
		foreach($errors as $error_row) {
			include($override_val);
		}
	}
	else {
		foreach($errors as $error_row) {
			if ($error_row['handle_file']!==FALSE &&
$error_row['handle_file']!=="") {
				include($error_row['handle_file']);
			}
			else {
				include($default_val);
			}
		}
	}
}

This works great as long as the array passed has more than one error (each
error being another array) in it. So if $errors[0] and $errors[1] both
exist, everything works. Where it gets really wacky is if only $errors[0]
exists then the foreach loops results in very strange results. Each error
array row in $errors consists of the following 8 indexes:
'err_no', 'err_text', 'err_file', 'err_line',
'system_err', 'handle_file',
'handle_code', 'severity'.
What happens is the foreach loops 8 times with $error_row being equal not
to the array in $errors[0] but being equal to an value in the sub array.
So the first result would be the equivalent of $errors[0]['err_no']'s
value. $errors is still an array, so why would the for each instead use
the sub array? very weird.
-- 
Edit bug report at http://bugs.php.net/?id=16071&edit=1
-- 
Fixed in CVS:        http://bugs.php.net/fix.php?id=16071&r=fixedcvs
Fixed in release:    http://bugs.php.net/fix.php?id=16071&r=alreadyfixed
Need backtrace:      http://bugs.php.net/fix.php?id=16071&r=needtrace
Try newer version:   http://bugs.php.net/fix.php?id=16071&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=16071&r=support
Expected behavior:   http://bugs.php.net/fix.php?id=16071&r=notwrong
Not enough info:     http://bugs.php.net/fix.php?id=16071&r=notenoughinfo
Submitted twice:     http://bugs.php.net/fix.php?id=16071&r=submittedtwice



Thread (5 messages)

« previous php.bugs (#2493) next »