Edit report at http://bugs.php.net/bug.php?id=54651&edit=1
ID: 54651
Comment by: felipecg00 at gmail dot com
Reported by: smartcoding at live dot com
Summary: Modify existing elements in Array created from
Object
Status: Open
Type: Bug
Package: Class/Object related
Operating System: *
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
I can reproduce it in 5.3.3.
Seems like it happens with numeric indexes only:
//create object
$obj->q = 'something';
//convert object to array
$arr = (array)$obj;
//modify element
$arr['q'] = 'something else';
//dump final array
print_r($arr);
Result:
Array
(
[q] => something else
)
This way works:
//create object
$obj = new stdClass(array(
1 => 'something'
));
//convert object to array
$arr = (array) $obj;
//modify element
$arr[1] = 'something else';
//dump final array
print_r($arr);
Results:
Array
(
[1] => something else
)
Previous Comments:
------------------------------------------------------------------------
[2011-05-02 15:51:39] smartcoding at live dot com
Description:
------------
After converting and Object to an associative Array using (array)
variable type
prefix cannot access and modify existing elements.
Tested in all latest PHP versions with and without modules.
Test script:
---------------
//create object
$obj->{1} = 'something';
//convert object to array
$arr = (array)$obj;
//modify element
$arr[1] = 'something else';
//dump final array
print_r($arr);
Expected result:
----------------
Array ( [1] => something else )
Actual result:
--------------
Array ( [1] => something [1] => something else )
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54651&edit=1