Getting data from an array?
My var_dump($gallery) looks like this:
array(1)
{ [0]=> object(stdClass)#102 (9) {
["term_id"]=> string(2) "17"
["name"]=> string(5) "Image"
["slug"]=> string(5) "image"
["term_group"]=> string(1) "0"
["term_taxonomy_id"]=> string(2) "19"
["taxonomy"]=> string(18) "gallery"
["description"]=> string(0) ""
["parent"]=> string
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et tempus tellus. Integer euismod, est et ultricies tristique, urna ipsum semper elit, pharetra cursus ligula turpis sed libero. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse pellentesque orci sed tellus hendrerit a auctor augue commodo. Ut nibh lacus, …
Read more...
(1) "0"
["count"]=> string(1) "1"
}
}
And I'm having trouble getting out data from the inside (in this case I want to echo "image"). For example:
$gallery[] outputs
Fatal erro开发者_JS百科r: Cannot use [] for reading in [source file url]
$gallery[0] shows
Catchable fatal error: Object of class stdClass could not be converted to string in [source file url]
$gallery[1], $gallery[2] and so forth are empty.
As far as I know PHP $gallery[0][3] should do the work but how, if I'm unable to echo stdClass object? :/ Is $gallery[0]['slug'] also valid btw?
Thanks a lot.
And yes - I'm unable to change the first item in the array, it's being generated by Wordpress, but I'm asking here because it's strict PHP question.
Cheers.
$gallery
is an array containing one object of type StdClass
.
You want to access the slug
member of the object held at index 0:
$gallery[0]->slug;
Full traversing like :
foreach ($gallery as $key=>$value)
{
print $key;
print $value;
}
Hope that helps :) And inside, you can get the first $key that would be the object and do it like $key->image
精彩评论