如何在 PHP 中打印 stdClass 对象的属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7762547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print properties of stdClass object in PHP?
提问by Brian Glaz
I have an array that contains standard class object. How can I return the properties (print them out) in a stdClass object?
我有一个包含标准类对象的数组。如何在 stdClass 对象中返回属性(打印出来)?
回答by Bailey Parker
If you just want to print you can use var_dump()
or print_r()
.
如果您只想打印,可以使用var_dump()
或print_r()
。
var_dump($obj);
print_r($obj);
If you want an array of all properties and their values use get_object_vars()
.
如果您想要一个包含所有属性及其值的数组,请使用get_object_vars()
.
$properties = get_object_vars($obj);
print_r($properties);
回答by Brian Glaz
you should be able to see them using var_dump($myObject);
你应该能够看到他们使用 var_dump($myObject);
回答by LeandroIP
I have solved this problem in this way:
我以这种方式解决了这个问题:
echo $arrayName[0]->varname;
As it is an array of objects, we use -> to access objects attributes/methods.
由于它是一个对象数组,我们使用 -> 来访问对象的属性/方法。