|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-05-02 15:51 UTC] 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 )
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Thu Jan 01 01:00:01 2026 UTC |
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 )