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

From: Date: Mon, 02 May 2011 14:17:51 +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-160506@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: 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

« previous php.bugs (#160506) next »