如何以字符串形式访问stdClass对象

时间:2014-01-17 15:05:25

标签: php stdclass

如何像字符串一样访问这些行?

来自Twitter的响应,并试图使用它。

stdClass Object
(
    [relationship] => stdClass Object
        (
            [source] => stdClass Object
                (
                    [id] => 2196933268
                    [id_str] => 2196933268
                    [screen_name] => damisleq
                    [following] => 1
                    [followed_by] => 
                    [notifications_enabled] => 
                    [can_dm] => 
                    [blocking] => 
                    [want_retweets] => 
                    [all_replies] => 
                    [marked_spam] => 
                )

            [target] => stdClass Object
                (
                    [id] => 175330071
                    [id_str] => 175330071
                    [screen_name] => sitetalkturkey
                    [following] => 
                    [followed_by] => 1
                )

        )

)

1 个答案:

答案 0 :(得分:1)

使用(string)方法将类型转换为类型字符串。例如:

$x = (string) $stdVar->relationship->source->screen_name;

var_dump($x); // will output string(n) "damisleq"
顺便说一下,如果从Twtter获得JSON结果,你也可以在json_decode中使用关联数组函数。例如。 $ result = json_decode($ string,true);你想要一个数组而不是stdObject的真实状态。