|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-04-23 04:36 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 08:00:01 2025 UTC |
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.<?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.