note 6609 deleted from unset by danbeck

From: Date: Wed, 20 Sep 2000 09:16:15 +0000
Subject: note 6609 deleted from unset by danbeck
Groups: php.notes 
Request: Send a blank email to php-notes+get-5214@lists.php.net to get a copy of this message
This function does reduce the index of an array, so if $joy has 5 elements and you do unset($joy[2]), the array will have only 4 elements. This means you should be careful when using <pre>for</pre> loops and the unset() function, like below: <pre>for ($i=0; $i<count($joy); $i++) { if ($joy[$i] != "fun") { unset($joy[i]); } }</pre> If the element isn't "fun", the count of $joy will be reduced by one, making your loop run fewer times than it should. A remedy would be to make a variable equal to the count ahead of time, like <pre>$joylen = count($joy);</pre>, then use that in the for loop instead.

« previous php.notes (#5214) next »