从数组中获取数据?
我的 var_dump($gallery) 看起来像这样:
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"
}
}
我在从内部获取数据时遇到问题(在本例中我想回显“图像”)。例如:
$gallery[] 输出
致命错误:无法使用[]读取[源文件url]
$gallery[0] 显示
可捕获的致命错误:类 stdClass 的对象无法转换为 [源文件 url] 中的字符串
$gallery[1]、$gallery[2] 等中的字符串为空。
据我所知 PHP $gallery[0][3] 应该完成这项工作,但是如果我无法回显 stdClass 对象,该怎么办? :/ 顺便说一句,$gallery[0]['slug'] 也有效吗?
多谢。
是的 - 我无法更改数组中的第一项,它是由 Wordpress 生成的,但我在这里问是因为这是严格的 PHP 问题。
干杯。
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 error: 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$gallery
是一个包含一个StdClass
类型对象的数组。您想要访问索引 0 处保存的对象的
slug
成员:$gallery
is an array containing one object of typeStdClass
.You want to access the
slug
member of the object held at index 0:完整的遍历如下:
希望有帮助:) 在里面,你可以得到第一个 $key 这将是对象,并像 $key->image 那样做
Full traversing like :
Hope that helps :) And inside, you can get the first $key that would be the object and do it like $key->image