#32088 [Fbk->Opn]: Reference fails after use of unset() on array

From: Date: Mon, 28 Feb 2005 23:50:36 +0000
Subject: #32088 [Fbk->Opn]: Reference fails after use of unset() on array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-74724@lists.php.net to get a copy of this message
 ID:               32088
 User updated by:  karl at posmaster dot com dot au
 Reported By:      karl at posmaster dot com dot au
-Status:           Feedback
+Status:           Open
 Bug Type:         Zend Engine 2 problem
 Operating System: *
 PHP Version:      5CVS-2005-02-28
 New Comment:

Although I can use a workaround for this problem (and in the meantime I
have), this is still a bug in php isn't it?

After using unset() on the array, in the loop, the reference breaks.


Previous Comments:
------------------------------------------------------------------------

[2005-02-28 22:57:00] tony2001@php.net

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);
?>

------------------------------------------------------------------------

[2005-02-28 22:49:56] karl at posmaster dot com dot au

- &$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);
?>

------------------------------------------------------------------------

[2005-02-28 20:18:52] sniper@php.net

What reference? Please give the _EXACT_ expected result.
(and shorten the example script..)


------------------------------------------------------------------------

[2005-02-27 22:51:05] karl at posmaster dot com dot au

No, the bug is still present.  The reference that should be present is
still broken.

------------------------------------------------------------------------

[2005-02-25 14:30:13] sniper@php.net

So it's fixed and working -> closed.


------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/32088

-- 
Edit this bug report at http://bugs.php.net/?id=32088&edit=1


Thread (1 message)

  • karl at posmaster dot com dot au
  • Unknown Message
    • karl at posmaster dot com dot au
« previous php.bugs (#74724) next »