I have a variable, "$terms", with the following contents/structure:
Array
(
[507] => stdClass Object
(
[term_id] => 507
[name] => Blog
[slug] => blog
[term_group] => 0
[term_taxonomy_id] => 679
[taxonomy] => blog-category
[description] =>
[parent] => 0
[count] => 3
[object_id] => 13665
)
[494] => stdClass Object
(
[term_id] => 494
[name] => ZMisc
[slug] => misc
[term_group] => 0
[term_taxonomy_id] => 662
[taxonomy] => blog-category
[description] =>
[parent] => 0
[count] => 5
[object_id] => 13665
)
)
I need to get the value of the first object's name. So in this instance, I need to retrieve the value of "Blog". This array is stored as $terms currently. I've tried $terms[0]->name among some other variants of this sytnax but can't quite get what I need.
In order to retrieve the first element of an array, you could do this:
var_dump(reset($terms));
There's many ways to do it:
current(reset($terms))->name;
reset($terms)->name; //thanks to comment from grossvogel, current is not needed
array_shift(array_values($terms))->name;
If you dont might modifying the original array, it can just as simple as
array_shift($terms)->name;