How to pass a multi dimensional array to a view in CodeIgniter

后端 未结 3 923
栀梦
栀梦 2021-01-24 00:36

this is really doing my nut in. I\'m passing a multidimensional array to a view like this:

$res = $this->deliciouslib->getRecentPosts();

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-24 01:33

    In CodeIgniter, when you pass an array to the view every key is set a simple variable:

     $data = array('foo' => 'bar');
     $this->load->view('myview', $data)
    
     // In your view
     echo $foo; // Will output "bar"
    

    So if you want to pass an array, just set a value as an array:

     $data = array('foo' => array('bar1', 'bar2') );
     $this->load->view('myview', $data)
    
     // In your view
     foreach($foo as $bar) {
       echo $bar . " "; // Will output "bar1 bar2 "
     }
    

提交回复
热议问题