PHP 数组 stdClass 对象 - 回显值


PHP Array stdClass Object - Echo Value

所以我把这个数组保存在一个变量标题$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[蛞蝓]

然后将显示"社区服务"。 我确信这很容易,但我无法弄清楚如何从数组 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 Object转换为php数组并打印任何值。

$php_array = json_encode($stdClass_object);
$php_array = json_decode($php_array,true);
echo "<pre>";print_r($php_array);