代码点火器:P在视图中传输数组数据


Codeigniter :Parsing array data in view

我正在尝试获取帖子数据并将其过去以使用这样的控制器查看

 $data['data']=$this->home->fetch_all_posts($this->news);
 $this->load->view('home',$data);

在我看来,当我做print_r($data)这就是我得到的

Array
(

    [0] => Array
        (
            [post] => stdClass Object
                (
                    [id] => 65
                    [date] => 2016-07-08
                    [title] =>  title of the news 
                    [content] => content of the news 
                    [type] => news
                )
            [attachments] => stdClass Object
                (
                    [id] => 2
                    [attachment_id] => 65
                    [type] => news
                    [path] => C:/wamp/www/school/ci/images/Jellyfish22.jpg
                )
        )
)

现在当我尝试为此并打印它不起作用时,请 helo 我该如何打印这些值

CodeIginiter 会将$data的值转换为自己的变量。因此,$data['foobar']在视图中变得$foobar。您对$data['data']的使用可能会令人困惑和模糊,我建议使用更具体的名称。

像这样尝试

 if (!empty($data)) {
   foreach ($data as $row) 
   {  
     $row['post']->id;
     $row['post']->title;
     $row['attachments']->id;
     $row['attachments']->attachment_id;
   }
 }

这应该是你所需要的。

$array = array(
  array('post'=> array(
           'id' => 65, 
           'date' => '2016-07-08',
           'title' => 'title of the news',
           'content' => 'content',
           'type' => 'news'
           ), 
        'attachments' => array(
           'id'=>2,
           'attachment_id' => 65, 
           'type'=> 'new', 
           'path' =>'C:/wamp'
         ))
  );
foreach ($array as $key => $value) {
  print_r($value['post']['id']); 
}