PHP 数组 stdClass 对象 - 回显值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10688650/
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
PHP Array stdClass Object - Echo Value
提问by user982853
so I have this array saved in a variable title $arr. I want to grab or echo the value of [slug]
所以我把这个数组保存在一个变量 title 中$arr。我想抓取或回显 [slug] 的值
Array ( [0] => stdClass Object ( [term_id] => 11
[name] => Community Service
[slug] => community-service
[term_group] => 0
[term_taxonomy_id] => 11
[taxonomy] => category
So i want something like this
所以我想要这样的东西
echo $arr[slug]
回声 $arr[slug]
which would then display "community-service". I am sure this is something pretty easy but i can't figure out how to grab the value out of an array stdClass and echo it on the page. Thank you.
然后将显示“社区服务”。我确信这很简单,但我不知道如何从数组 stdClass 中获取值并将其回显到页面上。谢谢你。
回答by ilanco
The array $arrcontains 1 item which is an object. You have to use the ->syntax to access it's attributes.
该数组$arr包含 1 个项目,它是一个对象。您必须使用->语法来访问它的属性。
echo $arr[0]->slug;
echo $arr[0]->slug;
回答by Pablo Ezequiel Leone
Sorry, but you can do something more elegant.
对不起,但你可以做一些更优雅的事情。
foreach ($array as $obj)
{
// Here you can access to every object value in the way that you want
echo $obj->term_id;
}
回答by tisuchi
Try simple following code...
尝试简单的以下代码...
echo $arr[0]->slug;
It supposed to be work because your array contain one object only.
它应该可以工作,因为您的数组只包含一个对象。
回答by George
It should work
echo $arr->{0}->slug
它应该工作
echo $arr->{0}->slug
回答by sandeep kumar
you can convert stdClass Object to php array like this and print any value.
您可以像这样将 stdClass 对象转换为 php 数组并打印任何值。
$php_array = json_encode($stdClass_object);
$php_array = json_decode($php_array,true);
echo "<pre>";print_r($php_array);

