所以我把这个数组保存在变量标题中 $arr. 。我想获取或回显 [slug] 的值

Array ( [0] => stdClass Object ( [term_id] => 11 
                                 [name] => Community Service 
                                 [slug] => community-service 
                                 [term_group] => 0 
                                 [term_taxonomy_id] => 11 
                                 [taxonomy] => category

所以我想要这样的东西

回声 $arr[slug]

然后将显示“社区服务”。我确信这是非常简单的事情,但我不知道如何从数组 stdClass 中获取值并将其回显在页面上。谢谢。

有帮助吗?

解决方案

数组 $arr 包含 1 个项目,该项目是一个对象。你必须使用 -> 语法来访问它的属性。

echo $arr[0]->slug;

其他提示

抱歉,但你可以做一些更优雅的事情。

foreach ($array as $obj)
{
    // Here you can access to every object value in the way that you want
    echo $obj->term_id;
}

尝试以下简单的代码...

echo $arr[0]->slug;

它应该是有效的,因为你的数组只包含一个对象。

应该有效echo $arr->{0}->slug

您可以像这样将 stdClass 对象转换为 php 数组并打印任何值。

$php_array = json_encode($stdClass_object);
$php_array = json_decode($php_array,true);
echo "<pre>";print_r($php_array);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top