php.net |  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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mazsolt at yahoo dot com
New email:
PHP Version: OS:

 

 [2007-04-15 17:19 UTC] mazsolt at yahoo dot com
Description:
------------
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;


Patches

Pull Requests

History

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

$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:
 http://www.php.net/manual/en/language.types.object.php#language.types.object.casting

and nothing new found
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 11:01:28 2024 UTC