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

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

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