0

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"?

Scott B
  • 35,095
  • 61
  • 148
  • 245
  • I am not really familiar with var_dump notation. Is that saying the 0th element of the array is an object with ID and guid attributes? Did you omit the other 4 elements? – erisco Nov 14 '10 at 00:07
  • @Scott B: as simple as `$excludeImages[0]->guid` ? – ajreal Nov 14 '10 at 00:07
  • @erisco: var_dump returns the entire contents of $excludeImages. There are 5 items in the array. I've just included the contents of the first item for simplicity/readability. – Scott B Nov 14 '10 at 00:13
  • @ajreal: yes, that yields the string. I just need the filename (avatar.png) and for each item in the array. Ultimately, I need to populate a 2nd array with all the filenames [avatar.png, somefile.jpg, anothefile.gif, etc...] – Scott B Nov 14 '10 at 00:16
  • As both answers suggest, use basename(). – Vincent Savard Nov 14 '10 at 00:19
  • @Scott B: as suggested by @jon_darkstar, basename, to safe guard, always `urlencode` query string appended after filename, otherwise, `basename("http://localhost/mysite/wp-content/uploads/avatar.png?q=/this_is_not_what_i_want/this_should_not_show")` will return `this_should_not_show` instead `avatar.png` – ajreal Nov 14 '10 at 00:25

2 Answers2

0

just use basename

$fileName = basename($excludeImages[0]->guid);

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

jon_darkstar
  • 15,278
  • 6
  • 24
  • 34
0

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 :

$path = $excludeImages[0]->guid;

If you only want to retrieve the filename, you can use basename() :

echo basename($path); // avatar.png

You can see this question for more information about stdClass.

Community
  • 1
  • 1
Vincent Savard
  • 30,767
  • 10
  • 61
  • 70