unset() bug or feature?
| From: | Johan Ekenberg | Date: | Sun, 02 Jan 2000 16:23:03 +0000 |
| Subject: | unset() bug or feature? | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-8810@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]?
$array = array("entry_0",
"entry_1");
$array_count = count($array);
for ($i=0;$i<$array_count;$i++) {
unset ($array[$i]);
$array[] = "entry_$i";
}
print "<P>count(\$array) = " . count($array);
print "<P>";
for ($i=0;$i<=3;$i++) {
print "\$array[$i] = $array[$i]<BR>";
}
Result:
count($array) = 2
$array[0] =
$array[1] =
$array[2] = entry_0
$array[3] = entry_1
Best regards,
/Johan Ekenberg