Re: SV: [PHP4BETA] unset() bug or feature?
| From: | Zeev Suraski | Date: | Mon, 03 Jan 2000 17:00:23 +0000 |
| Subject: | Re: SV: [PHP4BETA] unset() bug or feature? | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-8859@lists.php.net to get a copy of this message | ||
On Mon, 3 Jan 2000, Johan Ekenberg wrote:
> > > Mostly used to Perl, the result of the following code snippet
> > makes me a bit
> > > confused. I understand PHP makes no difference between arrays
> > and hashes,
> > > but in this case if feels somewhat awkward. Traversing $array, unsetting
> > > each value, adding new values, renders an array which doesn't
> > start at index
> > > 0. Shouldn't unset() really unset the entry, making the internal array
> > > pointer point to the next free place in $array (i.e. the place
> > of the just
> > > unset() entry)? In other words, if I unset() $array[0], why doesn't the
> > > subsequent $array[] = "entry_$i" put "entry_$i" at $array[0]?
> >
> > As far as I undestood it (from practical code) indexed arrays are just
> > a particular case for hashes/dictionaries/associative_arrays where the
> > keys
> > are an ints.
>
> Yes. But look at this:
>
> $array[0] = "Set";
> print count($array); // prints: 1
> unset ($array[0]); // $array now contains 0 items.
> reset($array); // This doesn't help
It doesn't help what?
> print count($array); // prints: 0
> $array[] = "Set"; // Add to $array (which contains _zero_ items!)
> print count($array); // prints: 1
> print "$array[0]"; // prints: (nothing)
> print "$array[1]"; // prints: Set
>
> Questions:
> 1. Why isn't the internal array pointer reset when any or all items are
> unset()?
Why should it? It should only move if the element being unset is the one
currently pointed by the internal pointer, and then too it shouldn't
reset, but move to the next element.
> 2. Why doesn't reset() reset the internal array pointer?
It does. What makes you think it doesn't?
Zeev
--
Zeev Suraski <zeev@zend.com>
http://www.zend.com/