note 25098 added to ref.array

From: Date: Tue, 10 Sep 2002 19:55:39 +0000
Subject: note 25098 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-36477@lists.php.net to get a copy of this message
As it has been stated before, array_pop and the splice-derived functions re-index any associatively defined elements.. // bad array example $array[5] = "foo"; $array[6] = "bar"; $var = array_pop($array); $key = key($array); // $key would be 0, not 5 as would be expected for the value "foo". Instead, keep your keys/associations by using the unset() function. // better array example end($array); $key = key($array); unset($array[$key]); // now the other keys/values aren't touched. ... This will remove the last entry in your array, but not re-index it. In this manner, you won't lose your associations. It has been tested and seems to work fine. -- http://www.php.net/manual/en/ref.array.php http://master.php.net/manage/user-notes.php?action=edit+25098 http://master.php.net/manage/user-notes.php?action=delete+25098 http://master.php.net/manage/user-notes.php?action=reject+25098

« previous php.notes (#36477) next »