Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the value from object(stdClass)?

Tags:

php

parsing

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.

like image 413
dwarbi Avatar asked Oct 31 '11 15:10

dwarbi


People also ask

How do you find the value of the stdClass object?

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!

What is stdClass object in PHP?

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.

How do I print a stdClass object?

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() .

How do you access the properties of an object in PHP?

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 .


1 Answers

You can do: $obj->Title etcetera.

Or you can turn it into an array:

$array = get_object_vars($obj);
like image 112
Naftali Avatar answered Oct 10 '22 15:10

Naftali