CodeIgniter Forums
How to echo this array? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to echo this array? (/showthread.php?tid=69117)



How to echo this array? - iridion2015 - 10-10-2017

Hi guys,

I send an array $data['content'] into view.

and here are the content of this array.

Code:
array(2) {
 [0]=>
 array(5) {
   ["subj_id"]=>
   string(1) "1"
   ["subject_name"]=>
   string(7) "english"
   ["photo_url"]=>
   string(0) ""
   ["author"]=>
   string(5) "jason"
   ["date_created"]=>
   string(10) "0000-00-00"
 }
 [1]=>
 array(5) {
   ["subj_id"]=>
   string(1) "2"
   ["subject_name"]=>
   string(4) "math"
   ["photo_url"]=>
   string(0) ""
   ["author"]=>
   string(4) "anna"
   ["date_created"]=>
   string(10) "0000-00-00"
 }
}



Now how to echo 'subject_name' and 'author' using foreach?


RE: How to echo this array? - PaulD - 10-10-2017

Well your question is a bit unclear. $data['content'] is the array $data with an element 'content'. Is the array you have shown the element 'content' or the array $data itself?

In your controller something like:
PHP Code:
public function index()
{
 
   $data = array();

 
   $content = array(
 
       array(
 
           'subject_name' => 'Maths',
 
           'subject_author' => 'Peter',
 
       ),
 
       array(
 
           'subject_name' => 'English',
 
           'subject_author' => 'John',
 
       ),
 
   );
 
   $data['content'] = $content;

 
   $this->load->view('homepage'$data);


In your 'homepage.php' view file something like:
PHP Code:
<?php if ( ! empty($content)) { ?>

     <?php foreach ($content as $item) { ?>

          <?php echo $item['subject_name']; ?>
          <?php echo $item['subject_author']; ?>

     <?php ?>

<?php ?>

I hope that helps but perhaps you were asking something more complicated that I have missed.

Best wishes

Paul.