php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60905 It's possible to get inconsistent arrays casting stdClass object to (array)
Submitted: 2012-01-27 16:09 UTC Modified: 2012-01-27 20:39 UTC
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mcahriman at gmail dot com Assigned:
Status: Duplicate Package: Scripting Engine problem
PHP Version: 5.3.9 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mcahriman at gmail dot com
New email:
PHP Version: OS:

 

 [2012-01-27 16:09 UTC] 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"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-27 20:38 UTC] felipe@php.net
Duplicated of bug #45959
 [2012-01-27 20:38 UTC] felipe@php.net
-Status: Open +Status: Not a bug
 [2012-01-27 20:39 UTC] felipe@php.net
-Status: Not a bug +Status: Duplicate
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 04:01:29 2024 UTC