|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54209 Inconsistent array index behaviour
Submitted: 2011-03-09 22:25 UTC Modified: 2011-03-11 04:52 UTC
From: phristen at yahoo dot com Assigned:
Status: Duplicate Package: Scripting Engine problem
PHP Version: 5.3.5 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: phristen at yahoo dot com
New email:
PHP Version: OS:


 [2011-03-09 22:25 UTC] phristen at yahoo dot com
It is well known that PHP casts numeric string indexes to integers, and it is normally impossible to force such indexes to remain strings. For example, the script below will output "integer":

$test = array("123" => 1);
echo gettype(key($test)); // integer

However, it is possible to achieve this if we assign numeric string property to an object (by using the ->{} syntax), and then cast it to array. Not only will the index retain it's string type, but it will also be impossible to access that index with [], which can't be correct behaviour. See attached example.

Test script:
$test = new stdClass();
$test->{"123"} = 1;
$test = (array)$test;

$key = key($test);
echo gettype($key);
echo "\n";
echo $test[$key];

Expected result:

Actual result:
PHP Notice: Undefined index: 123 in test.php on line 9


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-11 04:52 UTC]
-Status: Open +Status: Duplicate
 [2011-03-11 04:52 UTC]
Duplicate of numerous bugs, including bug #45959. In short, this is expected behaviour, and is unlikely to be changed, as doing so would cause performance issues within PHP.
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC