Bug #54651 [Com]: Modify existing elements in Array created from Object

From: Date: Mon, 02 May 2011 15:08:00 +0000
Subject: Bug #54651 [Com]: Modify existing elements in Array created from Object
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-160508@lists.php.net to get a copy of this message
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: You are right! Don't know why I thought that would work. Guess I need to sleep a bit. Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#160508) next »