怎么打印这个数组

时间:2013-09-10 16:14:20

标签: php arrays codeigniter

我想知道如何打印这个数组。此数组是使用此代码的print_f进行的调试。

function get_comments(){
        $data   = array();
        $posts  = array();
        $posts  = $this->ci->db->select('post_shared.id_post as post_id, post_shared.post_text, users.name,users.surname', false)
        ->join('post_user', 'post_user.id_post_shared = post_shared.id_post')
        ->join('users','users.id = post_user.user_id')
        ->order_by('id_post', 'desc')
        ->where('wall_id','2')
        ->get('post_shared', 10)
        ->result_array();   #get first 10 posts

        if( is_array( $posts ) && count( $posts ) > 0 ){
            foreach( $posts as $key=>$each ){
                ## gather the comments for the posts ###
                $comments   = array();
                $comments   = $this->ci->db->select('comment_text, id_user')->where('id_post', $each['post_id'])->get('post_comment')->result_array();
                if( is_array( $comments ) && count( $comments ) ){
                    $posts[$key]['comments']    = $comments;
                }
            }
        }
        return $posts;
    }

打印我使用的第一个数组

foreach ($result as $post => $each) {
       echo $each['name']; // this one work
    }

我的问题是打印注释数组,我该怎么做?感谢。

Array ( [post_id] => 4 [post_text] => Helloooo [name] => kasetta 
[surname] => yho [comments] => Array ( [0] => 
Array ( [comment_text] => yhooooo comment [id_user] => 2 ) [1] => Array ( [comment_text] => dasdasdsad [id_user] => 2 ) [2] => Array ( [comment_text] => yyu [id_user] => 2 ) ) )

1 个答案:

答案 0 :(得分:3)

假设我正确地阅读了你的例子......

foreach ($result as $post => $each) {
   echo $each['name']; // this one work
   echo '<br>';
   foreach($each['comments'] AS $c)
   {
        echo $c['comment_text'];
        echo '<br>';
        // user id would be $c['user_id'];
   }
}