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