1

if I do a print_r of my array I get :

Array ( 
    [0] => stdClass Object ( 
        [ID] => 34 
        [post_author] => 1 
        [post_date] => 2012-04-16 14:07:08 
        [post_date_gmt] => 2012-04-16 14:07:08 
        [post_content] => 
        [post_title] => visit_sfondo01 
        [post_excerpt] => 
        [post_status] => inherit 
        [comment_status] => open 
        [ping_status] => open 
        [post_password] => 
        [post_name] => visit_sfondo01-2 
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2012-04-16 14:07:08 
        [post_modified_gmt] => 2012-04-16 14:07:08 
        [post_content_filtered] => 
        [post_parent] => 1 
        [guid] => http://localhost:8080/pollo_blog/wp-content/uploads/2012/04/visit_sfondo011.jpg 
        [menu_order] => 0 
        [post_type] => attachment 
        [post_mime_type] => image/jpeg 
        [comment_count] => 0 
        [filter] => raw ) 
    [1] => stdClass Object ( 
        [ID] => 33 [post_author] => 1 
        [post_date] => 2012-04-16 13:53:35 
        [post_date_gmt] => 2012-04-16 13:53:35 
        [post_content] => 
        [post_title] => visit_gallery01 
        [post_excerpt] => 
        [post_status] => inherit 
        [comment_status] => open 
        [ping_status] => open 
        [post_password] => 
        [post_name] => visit_gallery01 
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2012-04-16 13:53:35 
        [post_modified_gmt] => 2012-04-16 13:53:35 
        [post_content_filtered] => 
        [post_parent] => 1 
        [guid] => http://localhost:8080/pollo_blog/wp-content/uploads/2012/04/visit_gallery01.jpg 
        [menu_order] => 0 
        [post_type] => attachment 
        [post_mime_type] => image/jpeg 
        [comment_count] => 0 
        [filter] => raw ) 
    )

and I'd like to access to the [guid] fields. Tried with :

echo $attachments[0]

or

but I don't show nothing (or I get, in the second example, an error).

Where am I wrong?

echo $attachments[0][guid]
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
markzzz
  • 47,390
  • 120
  • 299
  • 507

1 Answers1

3

It's a stdClass object in an array:

$attachments[0]->guid
Niko
  • 26,516
  • 9
  • 93
  • 110
  • and can I know how many guid elements there is into the array? – markzzz Apr 16 '12 at 14:16
  • @markzzz If you cannot be sure that every item in the $attachments array contains a "guid" key, use a simple loop to count: `$c = 0; foreach ($attachments as $a) { if (isset($a->guid)) { ++$c; } }` – Niko Apr 16 '12 at 14:25