如何在结构化数组中查找元素
下面的代码返回“附加”到帖子的图像数组...
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => 0
);
$excludeImages = get_posts($args);
//var_dump
var_dump ($excludeImages)
//yields(数组中第 5 项的片段)
array(5){[0]=> object(stdClass)#194 (24)
{["ID"]=> int(46)
["guid"]=> string(59) "http://localhost/mysite/wp-content/uploads/avatar.png"}
问题: 如何从模式始终为 ["guid"]=> 的任何给定数组项中提取图像文件名(在本例中为 avatar.png) string(int)“图像路径”?
The code below returns an array of images that are "attached" to posts...
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => 0
);
$excludeImages = get_posts($args);
//var_dump
var_dump ($excludeImages)
//yields (a snippet of the first item of 5 in the array)
array(5){[0]=> object(stdClass)#194 (24)
{["ID"]=> int(46)
["guid"]=> string(59) "http://localhost/mysite/wp-content/uploads/avatar.png"}
The Question:
How can I extract the image filename (in this case, avatar.png) from any given array item where the pattern is always ["guid"]=> string(int) "path-to-image"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用基本名称
编辑:正如上面的评论者提到的,我认为您可能需要箭头表示法,而不是像 $excludeImages[0]["guid"] 中那样为“guid”建立索引。我最初没有注意到它是一个对象。看来您有一个对象数组,而不是结构化/嵌套数组。
http://php.net/manual/en/function.basename.php
http://www.php.net/manual/en/function.pathinfo.php
just use basename
EDIT: As commenter above mentioned, i think you might need arrow notation instead of indexing for "guid" as in $excludeImages[0]["guid"]. I didn't notice initially that it is an object. It seems you have an array of objects, not a structured/nested array.
http://php.net/manual/en/function.basename.php
http://www.php.net/manual/en/function.pathinfo.php
stdClass 基本上是 PHP 的默认类(不是基础类!)。它没有任何方法或属性,它是一个空类。它被用作容器。例如,您可以通过将数组转换为对象来创建这样的类。
您的 var_dump 意味着数组的第一个元素是具有 ID 和 guid 属性的 stdClass 对象。因此,此代码将检索完整的 guid :
如果您只想检索文件名,可以使用 basename() :
您可以看到 这个问题了解有关 stdClass 的更多信息。
stdClass is basically the default class for PHP (not the base!). It does not have any method or attribute, it's an empty class. It is used as a container. You can create such a class by casting an array to an object, for example.
Your var_dump means that the first element of your array is an stdClass object having a ID and guid attributes. Therefore, this code would retrieve the full guid :
If you only want to retrieve the filename, you can use basename() :
You can see this question for more information about stdClass.