php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16071 foreach return bogus data on 1 row multi-dim array
Submitted: 2002-03-14 10:10 UTC Modified: 2002-08-07 01:00 UTC
Votes:7
Avg. Score:4.7 ± 0.7
Reproduced:7 of 7 (100.0%)
Same Version:2 (28.6%)
Same OS:1 (14.3%)
From: ned at wgtech dot com Assigned:
Status: No Feedback Package: Arrays related
PHP Version: 4.1.0 OS: Win 2000
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2002-03-14 10:10 UTC] ned at wgtech dot com
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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-14 10:49 UTC] ned at wgtech dot com
Noticed the errors not in foreach, but in the pass. PHP kills the outer array if theres only one element in it in a pass. Thats still a bug.
 [2002-07-03 21:12 UTC] eru@php.net
Sorry, but the bug system is not the appropriate forum for asking
support questions. Your problem does not imply a bug in PHP itself.
For a list of more appropriate places to ask for help using PHP,
please visit http://www.php.net/support.php

Thank you for your interest in PHP.

If you still think, that this is a bug, please minimalize your script, so that you can just reproduce the bug and then reopen the report.

Please take also into concideration, that the bug might already be fixed in a more current version of PHP.

Thank you.

 [2002-08-07 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2007-05-24 15:46 UTC] g dot agarwal123 at gmail dot com
print_r($data) results :

Array
(
[item] => Array
(
[stats] => Array
(
[0] => Array
(
[Date] => 05/23/2007
[ProgID] => 12
[Info] => 12
)

[1] => Array
(
[Date] => 05/01/2006
[ProgID] => 21
[Info] => 11
)

I tried using :

foreach ($data['item']['stats'] as $x)
{
echo $x['ProgID'];
}

It works fine as long as stats array has more than 1 array, but if the structure is :

Array
(
[item] => Array
(
[stats] => Array
(
[0] => Array
(
[Date] => 05/01/2006
[ProgID] => 12
[Info] => 12
)

Using same foreach loop, it shows wrong data.

Any ideas ?
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC