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

前端 未结 3 752
梦如初夏
梦如初夏 2020-12-09 07:18

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\"]         


        
相关标签:
3条回答
  • 2020-12-09 08:08

    You can do: $obj->Title etcetera.

    Or you can turn it into an array:

    $array = get_object_vars($obj);
    
    0 讨论(0)
  • 2020-12-09 08:13

    Example StdClass Object:

    $obj = new stdClass();
    
    $obj->foo = "bar";
    

    By Property (as other's have mentioned)

    echo $obj->foo; // -> "bar"
    

    By variable's value:

    $my_foo = 'foo';
    
    echo $obj->{$my_foo}; // -> "bar"
    
    0 讨论(0)
  • 2020-12-09 08:15

    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.

    Turning a StdClass object into an array:

    You can do this using the following code:

    $array = get_object_vars($obj);
    

    Take a look at: http://php.net/manual/en/language.oop5.magic.php http://krisjordan.com/dynamic-properties-in-php-with-stdclass

    0 讨论(0)
提交回复
热议问题