Edit report at https://bugs.php.net/bug.php?id=60905&edit=1

 ID:                 60905
 Updated by:         fel...@php.net
 Reported by:        mcahriman at gmail dot com
 Summary:            It's possible to get inconsistent arrays casting
                     stdClass object to (array)
-Status:             Not a bug
+Status:             Duplicate
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Linux
 PHP Version:        5.3.9
 Block user comment: N
 Private report:     N



Previous Comments:
------------------------------------------------------------------------
[2012-01-27 20:38:01] fel...@php.net

Duplicated of bug #45959

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

Reply via email to