Edit report at http://bugs.php.net/bug.php?id=54651&edit=1
ID: 54651 Updated by: [email protected] Reported by: smartcoding at live dot com Summary: Modify existing elements in Array created from Object -Status: Open +Status: Duplicate Type: Bug Package: Class/Object related Operating System: * PHP Version: 5.3.6 Block user comment: N Private report: N New Comment: This is documented behaviour, and a duplicate of bug #45959 -- in essence, it's a known issue, but not one that's likely to be fixed for performance reasons. Previous Comments: ------------------------------------------------------------------------ [2011-05-02 17:08:00] felipecg00 at gmail dot com You are right! Don't know why I thought that would work. Guess I need to sleep a bit. ------------------------------------------------------------------------ [2011-05-02 16:42:46] smartcoding at live dot com @felipecg00: 1) yes, this happens only with numeric indexes 2) in your second snippet you are creating an empty object and then assigning new element instead of modifying existing one. Dump your initial object in the beginning to see what I mean. ------------------------------------------------------------------------ [2011-05-02 16:17:51] felipecg00 at gmail dot com 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 ) ------------------------------------------------------------------------ [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
