#41169 [Opn->Asn]: foreach() on empty array causes key to be created

From: Date: Mon, 23 Apr 2007 09:20:11 +0000
Subject: #41169 [Opn->Asn]: foreach() on empty array causes key to be created
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-111806@lists.php.net to get a copy of this message
ID: 41169 Updated by: tony2001@php.net Reported By: mobile-g at softhome dot net -Status: Open +Status: Assigned Bug Type: Unknown/Other Function Operating System: Red Hat PHP Version: 4.4.6 -Assigned To: +Assigned To: derick New Comment: Assigned to the maintainer. Previous Comments: ------------------------------------------------------------------------ [2007-04-23 05:03:38] judas dot iscariote at gmail dot com <?php class foo { var $myArray = Array(); function foo() { foreach($this->myArray['value'] as $value){ } var_dump($this->myArray); } } $foo = new Foo(); produces the behaviuor you describe with PHP4 array(1) { ["value"]=> NULL } PHP5 works as expected. My suggestion for the reporter is to upgrade to PHP5 ASAP, you shouldnt be using PHP4 nowdays anyway. ------------------------------------------------------------------------ [2007-04-23 04:38:56] mobile-g at softhome dot net var $myArray = Array(); foreach($this -> mayArray['value'] as $value){ $myVAR += $value; //changed this line } actually it isn't asssigning to itself... this version is correct. ------------------------------------------------------------------------ [2007-04-23 04:36:57] mobile-g at softhome dot net Description: ------------ running a foreach loop on an empty array causes the test key to be created. Note this code has been pulled from a class, I have kept this structure in case that has relevance. Reproduce code: --------------- var $myArray = Array(); foreach($this -> mayArray['value'] as $value){ $myArray += $value; } Expected result: ---------------- Ideally the loop wouldn't run, or would error when trying to locate a none existant key. Actual result: -------------- myArray() has an empty key called "value" added to it. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=41169&edit=1

« previous php.bugs (#111806) next »