The PHP stdClass() is something that isn’t well documented but i will try to shed some light into the matter. stdClass is a default PHP object which has no predefined members. The name stdClass is used internally by Zend and is reserved. So that means that you cannot define a class named stdClass in your PHP code.
It can be used to manually instantiate generic objects which you can then set member variables for, this is useful for passing objects to other functions or methods which expect to take an object as an argument. An even more likely usage is casting an array to an object which takes each value in the array and adds it as a member variable with the name based on the key in the array.
Here’s an example below that converts an array to an object below. This method is called Type Casting.
<?php $person = array ( 'firstname' => 'Richard', 'lastname' => 'Castera' ); $p = (object) $person; echo $p->firstname; // Will print 'Richard' ?>
Here’s an example below that converts a multi-dimensional array to an object. This is accomplished through recursion.
<?php
function arrayToObject($array) {
if(!is_array($array)) {
return $array;
}
$object = new stdClass();
if (is_array($array) && count($array) > 0) {
foreach ($array as $name=>$value) {
$name = strtolower(trim($name));
if (!empty($name)) {
$object->$name = arrayToObject($value);
}
}
return $object;
}
else {
return FALSE;
}
}
?>
<?php
$person = array (
'first' => array('name' => 'Richard')
);
$p = arrayToObject($person);
?>
<?php // Now you can use $p like this: echo $p->first->name; // Will print 'Richard' ?>

Hey Richard, I really like your blog! It’s great! Also Google ads are very smoothly integrated.
Thanks!
$obj = new ArrayObject($array);
Hi Richard. This is very useful function to convert Array to stdClass object.
Please check out with:
$person = array (
‘first’ => array(‘name’ => array(‘first_name’ => ‘Richard’, ‘last_name’ => ‘Castera’) )
);
Thanks
Great post!
Just discovered that
$a = (object)mysql_fetch_assoc();
is faster than
$a = mysql_fetch_object();
I realy like this … thanks
I was looking for something like this today and found your blog. While I couldn’t get your solution to work (probably because I’m really tired at the moment
) but it did inspire me to create my own version which I thought I would share:
public function makeObjects($data) {
$object = (object) $data;
foreach ($object as $property) {
if (is_array($property)) $this->makeObjects($property);
}
return $object;
}
Its a little more compact and easier to follow I think. You can also embelish and add in the strtolower and trim for the properties if you like.
@Darryl, Thanks for sharing that!
You can also define your own class:
class arrayClass
{
public function __construct( $array = array() )
{
foreach( $array as $key => $value )
{
if( TRUE === is_array($value) )
$this->$key = new arrayClass( $value );
else
$this->$key = $value;
}
}
}
Then use it like this:
$array = array( ‘a’ => ‘abc’, ‘b’ => ‘xyz’, ‘c’ => array(‘one’ => 1, ‘two’ => 2) );
$obj = new arrayClass( $array );
print $obj->c->one; //prints ’1′
Great post Richard! I was having a problem with a class I needed to serialize to store the value but I needed dynamic variables and stdClass kept erroring out saying:
Fatal error: Cannot access empty propertyIt didn’t even occur that I should just create an assoc array and cast it to an object. Thanks for publishing this!
@terri, I’m happy you found this useful!
Thanks for sharing that!
@Tworzenie,
Very nice function indeed,
However I’m having a problem at this moment, it seems that when I call the function to convert an multidimensional associate array, it chops off the first element in the converted stdobject
I’ll let you know what I find out.
@Chang,
Please do. Thanks!
settype($array,’object’)
I too was having the same problem as Chang using this code where the first element of an multidimensional associate array was being lost.
The problem is in the test “!empty($name)”. If the array uses numeric indices, index zero is considered empty.
I changed “!empty($name)” to “!is_null($name)” and conversion worked as expected.
Interesting, all the samples that I found on the web use the !empty test and would all fail to convert for zero index.
Hope this helps.
I’m trying to implement this with CJ SOAP API, but I think CJ SOAP API is more complicated, I’m still not able to convert stdClass Object to Array.
@JB, Thanks for sharing that!
@Laurie, Thanks for sharing that!
@piwshop, Try reading a couple of the comments above, they might have a solution for you.
stdClass is an internal class name, not documented and so not recommended to use directly. it might even change in future php versions. instead, you should stick to converting arrays to object, or if you just need a blank object, convert a null to object.
@v Who recommends against using stClass? The Zend Framework uses it extensively, and it seems they’d have an inside track on such things.
With all due respect, many frameworks like Drupal will come to a screaming halt if using stdClass directly is disallowed so I think its safe to say that stdClass will be a usable term for the foreseeable future.
Buddy! You are best!
I waste 3 hr in looking for this.
Tnx very-very!
@Stas,
Hello Richard!
Just got here from Google, I am currently working on something to do with the Twitter API and that makes a array and stuff. And this just helped me to get the most recent status, I am very grateful, for this! Thank you!
Thank you, that has me very helped.
Cool, this is what i was looking for.
Man, Your the rock.