Bug #61655 [Opn->Nab]: Unable to access key in array cast from object
| From: | laruence@php.net | Date: | Mon, 09 Apr 2012 03:22:01 +0000 |
| Subject: | Bug #61655 [Opn->Nab]: Unable to access key in array cast from object | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-169344@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=61655&edit=1
ID: 61655
Updated by: laruence@php.net
Reported by: phpbugs at deep-freeze dot ca
Summary: Unable to access key in array cast from object
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: RedHat
PHP Version: 5.3.10
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
This is a documented behavior, plz lok at:
http://www.php.net/manual/en/language.types.array.php
Fixing that implies a perfomance decrease.
Previous Comments:
------------------------------------------------------------------------
[2012-04-09 02:52:20] reeze dot xia at gmail dot com
Hi,
in a object property lookup by name always in string, but in array numeric
string(like "22200" ) key will transform to numeric but not a string anymore.
when conversion internal HashTable did't changed so after conversion, key
lookup will fail.
as you posted:
array(1) {
["804496778"]=>
string(5) "Hello"
}
the key is string but not number.
if you do like this:
$array["1000"] = "value"
var_dump($array);
array(1) {
[1000]=>
string(5) "value"
}
see ? "1000" changed to 1000.
I've post a discussion about this in internal list. http://marc.info/?l=php-
internals&m=133390323614733&w=2
------------------------------------------------------------------------
[2012-04-07 08:14:11] googleclone at https dot webasyst dot net
http://www.blackberryempire.com/forum/buysell/single_ad.php?ad_id=6
http://posterous-spaces.posterous.com/
http://googleclone.tumblr.com/
http://googleclone.tumblr.com/
https://profiled.com/mobile/blog
------------------------------------------------------------------------
[2012-04-06 23:49:55] phpbugs at deep-freeze dot ca
Description:
------------
Casting from an object to an array with numeric key leaves the element inaccessible.
Given a object with a field whose name is numeric, after a cast to array, there is no means to
access the element.
That is $array['804496778'] yields an undefined error
array_key_exists(804496778, $array); yields false
array_key_exists('804496778', $array); fields false
yet a var_dump($array); shows this element;
and a array_keys($array); returns array(1) { [0] string(9) "804496778" }
Test script:
---------------
$object = new stdClass();
$key = '804496778';
$object->{$key} = "Hello;
$array = (array)$object;
echo $array[$key], ' ', (int)array_key_exists($key, $array), PHP_EOL;
var_dump($array)
Expected result:
----------------
Hello 1
array(1) {
["804496778"]=>
string(5) "Hello"
}
Actual result:
--------------
PHP Notice: Undefined index: 804496778 in /home/icewolf/bug.php on line 6
0
array(1) {
["804496778"]=>
string(5) "Hello"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61655&edit=1