note 37295 added to function.unset

From: Date: Sun, 09 Nov 2003 14:59:46 +0000
Subject: note 37295 added to function.unset
Groups: php.notes 
Request: Send a blank email to php-notes+get-60165@lists.php.net to get a copy of this message
Use array_values() after unset() to reindex your array. Note that unset() removes the index as a key, you will need to reindex your array again to get expected behavior <?php $petstring="cat,dog,rabbit,goldfish,hampster,budgie,mouse"; $testarray=explode(",",$petstring); //returns mouse echo "<br />".count($testarray)."<br />".$testarray[6]."<br />"; //remove the hampster unset($testarray[4]); //add a pet $testarray[]="parakeet"; //still returns mouse not parakeet! echo "<br />".count($testarray)."<br />".$testarray[6]."<br />"; //reindex array $testarray=array_values($testarray); //now returns parakeet echo "<br />".count($testarray)."<br />".$testarray[6]."<br />"; ?> ---- Manual Page -- http://www.php.net/manual/en/function.unset.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37295 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37295&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37295&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60165) next »