NigelRel3 Posted March 7, 2017 Share Posted March 7, 2017 I'm working with something where I use references to allow me to modify arrays - and the reference on a field seems to 'stick'.The code I've cut down to show this is... <?php $tableName = "BinType"; $fields = [ [ "name" => "binTypeID" ], [ "name" => "description" ] ]; foreach ( $fields as $field ) { echo " name1='" . $field ['name'] . "' "; } $fieldNames = array (); foreach ( $fields as &$field ) { echo " name2='" . $field ['name'] . "' "; $fieldNames [] = $field ['name']; } foreach ( $fields as $field ) { echo " name3='" . $field ['name'] . "' "; } Which gives as the output... name1='binTypeID' name1='description' name2='binTypeID' name2='description' name3='binTypeID' name3='binTypeID' So it seems that the reference in the middle foreach seems to be still in place for the last foreach.Can someone shed some light on this or do I just do what I did and rename the last $field to $field1 and ignore the problem? Thanks( PHP - PHP Version 7.0.15 ) Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 7, 2017 Share Posted March 7, 2017 When you are done with a reference variable, you can unset it. http://php.net/manual/en/language.references.unset.php Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 7, 2017 Share Posted March 7, 2017 Here is what I believe is happening: At the end of the second foreach() loop you have a "$field" variable that is a reference. Then on the first iteration of the third loop the "$fields as $field" within the foreach is taking the first value of the array $fields and setting it as the value of the variable $field - which is still a reference to the second element in the array. So, on that third foreach loop, I think it is actually displaying the value for the second element on both iterations - but you are seeing the first value because it was overwritten on the first instance. Think of it this way for the logic on the last foreach loop: Take the next value from the $fields array and assign it to the 2nd element of $fields. Then display the value of the 2nd element. Quote Link to comment Share on other sites More sharing options...
requinix Posted March 7, 2017 Share Posted March 7, 2017 So many people have reported that behavior as a bug in PHP that we added a big red warning in the documentation. It still amazes me how many people don't RTFM. Quote Link to comment Share on other sites More sharing options...
NigelRel3 Posted March 7, 2017 Author Share Posted March 7, 2017 Thanks - it makes sense if the value persists beyond the scope of the foreach. I was under the impression that this value is only valid inside the foreach block and therefore would be forgotten after each loop :-/ Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 8, 2017 Share Posted March 8, 2017 PHP doesn't have block scope. Statements like foreach, if etc. do not introduce a new variable scope (like they would in Java or C). Quote Link to comment Share on other sites More sharing options...
NigelRel3 Posted March 8, 2017 Author Share Posted March 8, 2017 My problem is that I've done more Java than PHP and so I was erroneously making that assumption about scope, I will now unscrew my head and make the appropriate adjustments Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.