PHP数组stdClass对象-回显值

时间:2022-10-23 07:54:39

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]

echo $ arr(弹头)

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中获取值并在页面上进行回显。谢谢你!

3 个解决方案

#1


33  

The array $arr contains 1 item which is an object. You have to use the -> syntax to access it's attributes.

数组$arr包含一个对象项。您必须使用->语法来访问它的属性。

echo $arr[0]->slug;

echo $ arr[0]- >弹头;

#2


4  

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;
}

#3


0  

Try simple following code...

试着简单代码…

echo $arr[0]->slug;

It supposed to be work because your array contain one object only.

它应该是工作的,因为数组只包含一个对象。

#1


33  

The array $arr contains 1 item which is an object. You have to use the -> syntax to access it's attributes.

数组$arr包含一个对象项。您必须使用->语法来访问它的属性。

echo $arr[0]->slug;

echo $ arr[0]- >弹头;

#2


4  

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;
}

#3


0  

Try simple following code...

试着简单代码…

echo $arr[0]->slug;

It supposed to be work because your array contain one object only.

它应该是工作的,因为数组只包含一个对象。