note 37535 added to language.types.array

From: Date: Wed, 19 Nov 2003 01:51:36 +0000
Subject: note 37535 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-60601@lists.php.net to get a copy of this message
Negative and positive array indices have different behavior when it comes to string<->int conversion. 1 and "1" are treated as identical indices, -1 and "-1" are not. So: $arr["1"] and $arr[1] refer to the same element. $arr["-1"] and $arr[-1] refer to different elements. The following code: <? $arr[1] = "blue"; $arr["1"] = "red"; $arr[-1] = "blue"; $arr["-1"] = "red"; var_dump($arr); ?> produces the output: array(3) { [1]=> string(3) "red" [-1]=> string(4) "blue" ["-1"]=> string(3) "red" } This code should create an array with either two or four elements. Which one should be the "correct" behavior is an exercise left to the reader.... ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37535 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37535&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37535&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60601) next »