Bug #60905 [NEW]: It's possible to get inconsistent arrays casting stdClass object to (array)
| From: | mcahriman at gmail dot com | Date: | Fri, 27 Jan 2012 16:09:12 +0000 |
| Subject: | Bug #60905 [NEW]: It's possible to get inconsistent arrays casting stdClass object to (array) | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-166860@lists.php.net to get a copy of this message | ||
From:
Operating system: Linux
PHP version: 5.3.9
Package: Scripting Engine problem
Bug Type: Bug
Bug description:It's possible to get inconsistent arrays casting stdClass object to (array)
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 bug report at https://bugs.php.net/bug.php?id=60905&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=60905&r=trysnapshot54
Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=60905&r=trysnapshot53
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=60905&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=60905&r=fixed
Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=60905&r=needdocs
Fixed in release: https://bugs.php.net/fix.php?id=60905&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=60905&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=60905&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=60905&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=60905&r=support
Expected behavior: https://bugs.php.net/fix.php?id=60905&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=60905&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=60905&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=60905&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=60905&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=60905&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=60905&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=60905&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=60905&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=60905&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=60905&r=mysqlcfg