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
 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC