Bug #60905 [Opn->Nab]: It's possible to get inconsistent arrays casting stdClass object to (array)
| From: | felipe@php.net | Date: | Fri, 27 Jan 2012 20:38:01 +0000 |
| Subject: | Bug #60905 [Opn->Nab]: It's possible to get inconsistent arrays casting stdClass object to (array) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-166868@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60905&edit=1
ID: 60905
Updated by: felipe@php.net
Reported by: mcahriman at gmail dot com
Summary: It's possible to get inconsistent arrays casting
stdClass object to (array)
-Status: Open
+Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Linux
PHP Version: 5.3.9
Block user comment: N
Private report: N
New Comment:
Duplicated of bug #45959
Previous Comments:
------------------------------------------------------------------------
[2012-01-27 16:09:12] mcahriman at gmail dot com
Description:
------------
When we cast stdClass with the numeric properties to array, we cannot access the
value by key. That seems, that this bug is connected with the Bug #60379 where
casting to (object) is performed. Such objects can be got by json_decode()
function
Test script:
---------------
<?
$obj = new stdClass();
$obj->{'4001'} = 'foo';
$obj->{'4002'} = 'bar';
$obj->{'4003'} = 'baz';
$arr = (array) $obj;
print_r($arr);
foreach($arr as $key => $val) {
printf('In foreach "$key => $val" key is "%s", val is "%s"
but $arr[$key] is "%s" should be "%s"', $key, $val, $arr[$key], $val);
print("\n");
}
Expected result:
----------------
Array
(
[4001] => foo
[4002] => bar
[4003] => baz
)
....
In foreach "$key => $val" key is "4003", val is "baz" but
$arr[$key] is "baz"
should be "baz"
Actual result:
--------------
Array
(
[4001] => foo
[4002] => bar
[4003] => baz
)
PHP Notice: Undefined index: 4001 in /home/manfred/poc.php on line 13
PHP Stack trace:
PHP 1. {main}() /home/manfred/poc.php:0
In foreach "$key => $val" key is "4001", val is "foo" but
$arr[$key] is ""
should be "foo"
PHP Notice: Undefined index: 4002 in /home/manfred/poc.php on line 13
PHP Stack trace:
PHP 1. {main}() /home/manfred/poc.php:0
In foreach "$key => $val" key is "4002", val is "bar" but
$arr[$key] is ""
should be "bar"
PHP Notice: Undefined index: 4003 in /home/manfred/poc.php on line 13
PHP Stack trace:
PHP 1. {main}() /home/manfred/poc.php:0
In foreach "$key => $val" key is "4003", val is "baz" but
$arr[$key] is ""
should be "baz"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60905&edit=1