从数组中获取数据?

发布于 2024-10-07 05:47:06 字数 1525 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼亦尘 2024-10-14 05:47:06

$gallery 是一个包含一个 StdClass 类型对象的数组。

您想要访问索引 0 处保存的对象的 slug 成员:

$gallery[0]->slug;

$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;
记忆里有你的影子 2024-10-14 05:47:06

完整的遍历如下:

foreach ($gallery as $key=>$value)
{
  print $key;
  print $value;
}

希望有帮助:) 在里面,你可以得到第一个 $key 这将是对象,并像 $key->image 那样做

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文