|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-02-23 23:30 UTC] karl at posmaster dot com dot au
Description:
------------
Using "for each" I wanted to remove some items and change others. It seems that the reference in the loop no longer works once unset() has been called on the array.
The commented line achieves the desired end, but circumvents the reference.
The reference works if the unset() line is commented.
This is reproduced without the use of Zend or 3rd part extensions.
-----------------------
Reproduce code:
---------------
<?php
$stuff = array(array('one','two'),array('one','two'),
array('three','four'),array('five','six'),
array('seven','eight'),array('nine','ten'));
var_dump($stuff);
foreach ($stuff as $key => &$values) {
print "on key:$key<br>";
if(($key%2)==0){
print "Running unset for $key <br>";
unset ($stuff[$key]);
}else{
print "Running change for $key <br>";
$values[1]='foo';
// $stuff[$key][1] = 'foo';
}
}
var_dump($stuff);
?>
Expected result:
----------------
I expected to see elements $stuff[1][1], $stuff[3][1] and $stuff[5][1] to be set to 'foo'
Actual result:
--------------
The elements are left unchanged. Are values 'two', 'six' and 'ten' respectively.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 28 07:00:01 2025 UTC |
- &$value is a refernce used in the foreach loop. - If the unset is commented out, the reference to $stuff[1] as $value on the second iteration of the loop works. - The call to unset() on the first iteration breaks the reference Actaul Output: array(1) { [1]=> string(3) "two" } Expected Output array(1) { [1]=> string(3) "This should appear below in the var_dump() because $value is passed by reference" } <?php $stuff = array('one','two'); foreach ($stuff as $key => &$value) { if($key==0){ unset ($stuff[$key]); }else{ $value='This should appear below in the var_dump() because $value is passed by reference'; } } var_dump($stuff); ?>You are modifying array in the foreach loop. Consider using for/while instead of foreach. See this code: <?php $stuff = array('one','two'); foreach ($stuff as $key => &$value) { $value='This should appear below in the var_dump() because $value is passed by reference'; } var_dump($stuff); ?>Even more fun: Try this variant and you'll see that $fluff not only gets reset to NULL, it gets completely unset. <?php $stuff = array('one','two'); $fluff = &$stff; var_dump($stuff); var_dump($fluff); foreach ($stuff as $key => &$value) { if($key==0){ unset ($stuff[$key]); }else{ $value='This should appear below in the var_dump() because $value is passed by reference'; } } var_dump($stuff); var_dump($fluff); ?>