php.net |  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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phristen at yahoo dot com
New email:
PHP Version: OS:

 

 [2011-03-09 22:25 UTC] phristen at yahoo dot com
Description:
------------
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:
---------------
<?php
$test = new stdClass();
$test->{"123"} = 1;
$test = (array)$test;

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

Expected result:
----------------
integer
1

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-11 04:52 UTC] aharvey@php.net
-Status: Open +Status: Duplicate
 [2011-03-11 04:52 UTC] aharvey@php.net
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-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC