Re: Possible foreach bug; seeking advice to isolate the problem
| From: | Gary | Date: | Wed, 20 Oct 2010 12:49:47 +0000 |
| Subject: | Re: Possible foreach bug; seeking advice to isolate the problem | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-308883@lists.php.net to get a copy of this message | ||
Tommy Pham wrote:
>> -----Original Message-----
>> From: Gary
>> I can tell you how to solve it:
>> $a = array('a', 'b','c');
>> foreach($a as &$row){
>> //you don't have to do anything here
>> }
>> unset($row); // <----<<< THIS IS KEY!
>
> Shouldn't that be $row = null since unset will remove the last value,
> not just removing the variable also, from the array whereas the $row =
> null will tell the reference pointer that it doesn't point to a value.
,----[ http://php.net/manual/en/control-structures.foreach.php
]
| ,----[ code ]
| | <?php
| | $arr = array(1, 2, 3, 4);
| | foreach ($arr as &$value) {
| | $value = $value * 2;
| | }
| | // $arr is now array(2, 4, 6, 8)
| | unset($value); // break the reference with the last element
| | ?>
| `----
| ...
| Warning
|
| Reference of a $value and the last array element remain even after the
| foreach loop. It is recommended to destroy it by unset().
`----
If it's wrong, please blame the people who wrote the manual :)