stdclass
How to access a property of an object (stdClass Object) member/element of an array? [duplicate]
To access an array member you use $array[‘KEY’]; To access an object member you use $obj->KEY; To access an object member inside an array of objects: $array[0] // Get the first object in the array $array[0]->KEY // then access its key You may also loop over an array of objects like so: foreach ($arrayOfObjs as … Read more
PHP – recursive Array to Object?
As far as I can tell, there is no prebuilt solution for this, so you can just roll your own: function array_to_object($array) { $obj = new stdClass(); foreach ($array as $k => $v) { if (strlen($k)) { if (is_array($v)) { $obj->{$k} = array_to_object($v); //RECURSION } else { $obj->{$k} = $v; } } } return $obj; … Read more
Object of class stdClass could not be converted to string
Most likely, the userdata() function is returning an object, not a string. Look into the documentation (or var_dump the return value) to find out which value you need to use.
When should I use stdClass and when should I use an array in php oo code?
The usual approach is Use objects when returning a defined data structure with fixed branches: $person -> name = “John” -> surname = “Miller” -> address = “123 Fake St” Use arrays when returning a list: “John Miller” “Peter Miller” “Josh Swanson” “Harry Miller” Use an array of objects when returning a list of structured … Read more
Convert/cast an stdClass object to another class
See the manual on Type Juggling on possible casts. The casts allowed are: (int), (integer) – cast to integer (bool), (boolean) – cast to boolean (float), (double), (real) – cast to float (string) – cast to string (array) – cast to array (object) – cast to object (unset) – cast to NULL (PHP 5) You … Read more
How to convert an array into an object using stdClass() [duplicate]
You just add this code $clasa = (object) array( ‘e1’ => array(‘nume’ => ‘Nitu’, ‘prenume’ => ‘Andrei’, ‘sex’ => ‘m’, ‘varsta’ => 23), ‘e2’ => array(‘nume’ => ‘Nae’, ‘prenume’ => ‘Ionel’, ‘sex’ => ‘m’, ‘varsta’ => 27), ‘e3’ => array(‘nume’ => ‘Noman’, ‘prenume’ => ‘Alice’, ‘sex’ => ‘f’, ‘varsta’ => 22), ‘e4’ => array(‘nume’ => … Read more
PHP: Count a stdClass object
The problem is that count is intended to count the indexes in an array, not the properties on an object, (unless it’s a custom object that implements the Countable interface). Try casting the object, like below, as an array and seeing if that helps. $total = count((array)$obj); Simply casting an object as an array won’t … Read more
What is stdClass in PHP?
stdClass is just a generic ’empty’ class that’s used when casting other types to objects. Despite what the other two answers say, stdClass is not the base class for objects in PHP. This can be demonstrated fairly easily: class Foo{} $foo = new Foo(); echo ($foo instanceof stdClass)?’Y’:’N’; // outputs ‘N’ I don’t believe there’s … Read more