You are here: Re: safe to delete elements of array in foreach « PHP Programming Language « IT news, forums, messages
Re: safe to delete elements of array in foreach

Posted by gosha bine on 05/21/07 17:39

On 21.05.2007 18:00 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
>

Good point. Here's another example for even deeper meditation

$a = range(0,4);
// $b = &$a;
foreach($a as $k => $v){
$a[99] = 5;
echo "$k=>$v\n";
}

(try uncommenting the second line).




--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация