|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41095 array conversion to object
Submitted: 2007-04-15 17:19 UTC Modified: 2007-04-15 18:30 UTC
From: mazsolt at yahoo dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.1 OS: Win
Private report: No CVE-ID: None
 [2007-04-15 17:19 UTC] mazsolt at yahoo dot com
I tried to convert an array, with integer keys, to an object. The problem seems to be that conversion is done, but the object's elements can't be called by the script. 

$obj = (object)array(1,2,3);
var_dump($obj) returns: object(stdClass)#1 (3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) 
var_export($obj) returns:  stdClass::__set_state(array( ))

OK, the conversion is done, but objects don't support $obj->0 property access (property names should be valid php identifiers). 
When it's not a bug, is there a way to call that property? 
Except the object iteration pattern? I consulted some forums, but nowhere have got an answer.

Reproduce code:
$obj = (object)array(1,2,3);

// it works
foreach($obj as $key => $value) {
    echo "$key => $value";

// doesn't work
echo $obj->0;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-15 17:46 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

$obj->{0} should help. See documentation for more information.
 [2007-04-15 18:30 UTC] mazsolt at yahoo dot com
I tried with:
echo $obj->{0};

and I got the message:

Notice: Undefined property: stdClass::$0 in D:\Apache Group\Apache2\htdocs\test.php

Consulted the docs at:

and nothing new found
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 15:01:30 2024 UTC