不能从数组中获取对象,什么都不输出,数组print_r工作正常

时间:2012-07-29 14:59:29

标签: php arrays

我确定有一些愚蠢的遗漏,但我很累,无法理解究竟是什么。

我有一个print_rs就好的数组:

Array ( [0] => stdClass Object ( [comment_ID] => 20 [comment_post_ID] => 227 [comment_author] => admin [comment_author_email] => [comment_author_url] => [comment_author_IP] => ::1 [comment_date] => 2012-07-29 14:19:34 [comment_date_gmt] => 2012-07-29 14:19:34 [comment_content] => I'm attending this Event! [comment_karma] => 0 [comment_approved] => 1 [comment_agent] => [comment_type] => [comment_parent] => 0 [user_id] => 1 ) )

P.S。删除了一些隐私价值。

现在我想获得comment_ID,但没有输出到浏览器。

php看起来像这样:

$current_user_comment = get_comments( array(
  'post_id' => $post->ID,
  'user_id' => get_current_user_id(),
  'number' => 1,
  'status' => 'approve',
  'type' => 'comment'
) );

print_r ( $current_user_comment );
echo $current_user_comment->comment_ID;

1 个答案:

答案 0 :(得分:2)

print_r的输出中可以看出,$current_user_comment是数组,而不是对象。期望的对象位于该数组中的索引0

$current_user_comment[0]->commentID将有效。