Using PHP, I have to parse a string coming to my code in a format like this:
object(stdClass)(4) {
["Title"]=> string(5) "Fruit"
["Color"]=> string(6) "yellow"
["Name"]=> string(6) "banana"
["id"]=> int(3)
}
I'm sure there's a simple solution, but I can't seem to find it... how to get the Color and Name?
Thanks so much.
You create StdClass objects and access methods from them like so: $obj = new StdClass; $obj->foo = "bar"; echo $obj->foo; I recommend subclassing StdClass or creating your own generic class so you can provide your own methods. Thank you for your help!
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() .
Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property .
You can do: $obj->Title
etcetera.
Or you can turn it into an array:
$array = get_object_vars($obj);
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