本文介绍了PHP基础:如何访问对象属性的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在变量上执行var_dump($ myvar)时,会显示此输出.

I have this output when I do var_dump($myvar) on a variable.

    object(stdClass)#5 (19) {
      ["contributors"]=>
      NULL
      ["coordinates"]=>
      NULL
      ...
      ...
      ...
      ["text"]=>
      string(118) "Tune in to @Current TV this Saturday for post-debate commentary from me + @JenGranholm + Cenk Uygur #PoliticallyDirect"
    }

如何到达文本"属性?我以为是$ myvar ["text"],但这给了我这个错误信息:

How do I reach the "text" attribute? I thought it would be $myvar["text"] but that gives me this error message:

致命错误:不能将stdClass类型的对象用作数组

Fatal error: Cannot use object of type stdClass as array

推荐答案

如果成员名称很简单,则可以使用->运算符:

If the member names are simple, you can use the -> operator:

echo $myvar->text;

您可以使用其他语法来访问包含特殊字符的成员名称(JSON解码的数据通常会产生这种情况):

You can use an alternate syntax to access members names that contain special characters (JSON decoded data often produces such cases):

echo $myvar->{'some-other-text-with-hyphens'};

这篇关于PHP基础:如何访问对象属性的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 08:33