PHP Array stdClass Object - Echo Value

て烟熏妆下的殇ゞ 提交于 2019-12-04 18:12:49

问题


so I have this array saved in a variable title $arr. I want to grab or echo the value of [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]

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.


回答1:


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

echo $arr[0]->slug;




回答2:


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:


Try simple following code...

echo $arr[0]->slug;

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




回答4:


It should work echo $arr->{0}->slug




回答5:


you can convert stdClass Object to php array like this and print any value.

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


来源:https://stackoverflow.com/questions/10688650/php-array-stdclass-object-echo-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!