SV: [PHP4BETA] unset() bug or feature?
| From: | Johan Ekenberg | Date: | Mon, 03 Jan 2000 16:51:23 +0000 |
| Subject: | SV: [PHP4BETA] unset() bug or feature? | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-8858@lists.php.net to get a copy of this message | ||
> > 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
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()?
2. Why doesn't reset() reset the internal array pointer?
PHP manual: "reset() rewinds array's internal pointer to the first
element."
/Johan Ekenberg