|
Posted by Jerry Stuckle on 05/21/07 19:55
Rami Elomaa wrote:
> Jerry Stuckle kirjoitti:
>> Rami Elomaa wrote:
>>> gosha bine kirjoitti:
>>>> On 20.05.2007 05:09 Jon Slaughter wrote:
>>>>
>>>>>
>>>>> Well, your right. I assume that when it says copy it means a deep
>>>>> copy. If so then essentially its working on a different variable...
>>>>
>>>> From my understanding, foreach doesn't make a copy of *entire
>>>> array*, it just copies *current value* into the loop variable
>>>> (unless you provided it by reference).
>>>
>>> Here's a way to test it:
>>>
>>> <?php
>>>
>>> $a = range(0,4);
>>>
>>> foreach($a as $k => $v){
>>> if(isset($a)) unset($a);
>>> echo "$k => $v<br>";
>>> }
>>>
>>> print_r($a);
>>>
>>> ?>
>>>
>>> To me it would seem the entire array is copied, since it prints all
>>> the values even after the array is unset, I got the output:
>>> 0 => 0
>>> 1 => 1
>>> 2 => 2
>>> 3 => 3
>>> 4 => 4
>>>
>>
>> Rami,
>>
>> That may be true on the release you're running at. But is it true for
>> all releases? And will it remain that way? Unless it's documented to
>> work that way, there's no way of telling.
>
> I agree that if it is documented you can't trust it. However, looking at
> http://fi2.php.net/manual/en/control-structures.foreach.php you'll find
> the following: "Note: Unless the array is referenced, foreach operates
> on a copy of the specified array and not the array itself." Wouldn't
> that make this a _documented_ feature that you can actually rely on?
>
> --
> Rami.Elomaa@gmail.com
>
> "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
> usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Yes, but that wasn't the original question.
The original question was - is it safe to delete from the original array
while in a foreach() loop? The answer to this is still no - there is no
indication what happens with the copy when the original is changed.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|