|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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;
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 04:00:01 2025 UTC |
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