Re: Bug or undocumented functionality of unset()?

From: Date: Wed, 23 Feb 2005 15:40:00 +0000
Subject: Re: Bug or undocumented functionality of unset()?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-209242@lists.php.net to get a copy of this message
karl@posmaster.com.au wrote: > Hi All, > > Does unset() create a copy of the array and then overwrite the > original > somehow > destroying the reference? ... > ----------------------------------------------- > <?php > $stuff = array(array('one','two'), > array('one','two'), > array('three','four'), > array('five','six'), > array('seven','eight'), > array('nine','ten')); > > print '<pre>'; print_r($stuff); print '</pre>'; > > foreach ($stuff as $key => &$values) { This kind of array manipulation only works in PHP5... which I assume you are using? > print "on key:$key<br>"; > if(($key%2)==0){ > print "Running unset for $key <br>"; > unset ($stuff[$key]); This works fine for me... again, using PHP5 here. > }else{ > print "Running change for $key <br>"; > $values[1]='foo'; Now this is an interesting problem. It seems that $values[1] is not being changed. I don't know if this is a quirk or not, but since $values should be a reference I would expect it to work for values that are arrays (and not just scalars). Check out bugs.php.net for this part of the problem. Seems the root issue here is that array references only work for scalar values. This is also a PHP5-only issue (since foreach references wasn't a feature of PHP4). > // $stuff[$key][1] = 'foo'; Using foreach in PHP4 you would have to do something like the line above to change a value. It appears that this is also the only way to do it in PHP5? ... but IMO this shouldn't be the way it is... > } > } > print '<pre>'; print_r($stuff); print '</pre>'; > ?> -- Teach a man to fish... NEW? | http://www.catb.org/~esr/faqs/smart-questions.html STFA | http://marc.theaimsgroup.com/?l=php-general&w=2 STFM | http://php.net/manual/en/index.php STFW | http://www.google.com/search?q=php LAZY | http://mycroft.mozdev.org/download.html?name=PHP&submitform=Find+search+plugins

Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc
« previous php.general (#209242) next »