note 37295 modified in function.unset by tomsommer

From: Date: Mon, 28 Jun 2004 15:20:21 +0000
Subject: note 37295 modified in function.unset by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72050@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 --was-- 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 />"; ?> http://php.net/manual/en/function.unset.php

« previous php.notes (#72050) next »