2012-04-16 39 views
1

如果我做我的数组的print_r的获取:如何访问数组中的此元素?

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) 
    ) 

,我想访问到[GUID]字段。与尝试:

echo $attachments[0] 

,但我没有什么不显示(或我得到的,在第二个例子中,一个错误)。

我在哪里错了?

echo $attachments[0][guid] 

回答

3

这是在数组中的stdClass的对象:

$attachments[0]->guid 
+0

,我可以知道有多少GUID元素存在入阵? – markzzz 2012-04-16 14:16:35

+1

http://stackoverflow.com/questions/1314745/php-count-an-stdclass-object – 2012-04-16 14:17:37

+0

@markzzz如果您不能确定$ attachments数组中的每个项目都包含“guid”键,请使用简单的循环计数:'$ c = 0; foreach($ attachments as $ a){if(isset($ a-> guid)){++ $ c; }}' – Niko 2012-04-16 14:25:45