i have:
stdClass Object ( [0] => stdClass Object ( [one] => aaa [two] => sss ) [1] => stdClass Object ( [one] => ddd [two] => fff ) [2] => stdClass Object ( [one] => ggg [two] => hhh ) }
and i must get this with keys, for example:
$var = $stdClass[0];
but i have error:
Fatal error: Cannot use object of type stdClass as array in
Is possible parse this stdClass to array and use this with keys?
The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.
If you just want to print you can use var_dump() or print_r() . var_dump($obj); print_r($obj); If you want an array of all properties and their values use get_object_vars() .
Cast it to an array:
$array = (array)$stdClass;
If you're using json_decode
to convert that JSON string into an object, you can use the second parameter json_decode($string, true)
and that will convert the object to an associative array.
If not, what everybody else has said and just type cast it
$array = (array) $stdClass;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With