oddity in array indexes

From: Date: Wed, 30 Apr 2003 15:05:58 +0000
Subject: oddity in array indexes
Groups: php.general 
Request: Send a blank email to php-general+get-145789@lists.php.net to get a copy of this message
Hi, I have found an oddity in the way PHP 4.3.1 does array indexes, and wonder if others can replicate it for me. Here is some test code: <? print '<pre>'; $arr = array(1, 2, 3, 4); print_r($arr); $val = array_shift($arr); print_r($arr); $arr[] = $val; print_r($arr); print '</pre>'; ?> I'm interested in the array indices in the printout. When I run that code on a server running 4.3.1, the output is: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) Array ( [0] => 2 [1] => 3 [2] => 4 ) Array ( [0] => 2 [1] => 3 [2] => 4 [4] => 1 ) Note the last array printout -- the last index is 4, rather than 3. However, if you run that identical code on an earlier version of PHP the indices display how I would expect them to: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) Array ( [0] => 2 [1] => 3 [2] => 4 ) Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 1 ) Also, if you do further array_shift() and array_push() or [] operations, the indices don't get out of whack further -- that is, the last index always remains at 4, but the first three are 0-2 as expected. Is this a bug, or just me missing something painfully obvious? Do other folks see this behavior on recent-ish builds of PHP? I don't have 4.3.2 handy to try it out on... -- [ joel boonstra | jboonstra@gospelcom.net ]

« previous php.general (#145789) next »