title: Echo arrays in foreach loop codeigniter
body: <p>I've been using codeigniter for years but there's a really big gap in between so i always find myself in situations where i forgot how to do things and its almost midnight here so my brain isn't working fast. Can someone show me how to echo the array i have and explain to me how they are processed in the foreach loops?</p>
  
  <p>I have this code in my model to take the rows in 2 tables.</p>
  
  <pre><code>public function tag_genre(){
  $result['tag'] = $this-&gt;db-&gt;get('tags')-&gt;result_array();
  $result['genre'] = $this-&gt;db-&gt;get('genre')-&gt;result_array();
  return $result;
  }
  </code></pre>
  
  <p>And I have this in my controller</p>
  
  <pre><code>public function view_publish_story(){
  $data = array('tag_genre' =&gt; $this-&gt;story_model-&gt;tag_genre(), 'title' =&gt; "New Story");
  
  $this-&gt;load-&gt;view('template/header',$data);
  $this-&gt;load-&gt;view('template/navbar');
  $this-&gt;load-&gt;view('pages/storypublish',$data);
  $this-&gt;load-&gt;view('template/footer');   
  }
  </code></pre>
  
  <p>I used print_r in my view and this is the result. Seeing this just confuses me more. It's been atleast 2 years since i even dealt with foreach loops.</p>
  
  <blockquote>
    <p>Array ( [tag] => Array ( [0] => Array ( [tag_id] => 1 [tag_name] =>
    LitRPG ) [1] => Array ( [tag_id] => 2 [tag_name] => Virtual Reality )
    [2] => Array ( [tag_id] => 3 [tag_name] => Cyberpunk ) [3] => Array (
    [tag_id] => 4 [tag_name] => Reincarnation ) [4] => Array ( [tag_id] =>
    5 [tag_name] => Summoned Hero ) [5] => Array ( [tag_id] => 6
    [tag_name] => Martial Arts ) [6] => Array ( [tag_id] => 7 [tag_name]
    => Slice of Life ) [7] => Array ( [tag_id] => 8 [tag_name] => Overpowered ) [8] => Array ( [tag_id] => 9 [tag_name] => Non-Human )
    [9] => Array ( [tag_id] => 10 [tag_name] => Anti-hero ) ) [genre] =>
    Array ( [0] => Array ( [genre_id] => 1 [genre_name] => action ) [1] =>
    Array ( [genre_id] => 2 [genre_name] => adventure ) [2] => Array (
    [genre_id] => 3 [genre_name] => comedy ) [3] => Array ( [genre_id] =>
    4 [genre_name] => Drama ) [4] => Array ( [genre_id] => 5 [genre_name]
    => Fantasy ) [5] => Array ( [genre_id] => 6 [genre_name] => Historical ) [6] => Array ( [genre_id] => 7 [genre_name] => Horror ) [7] => Array
    ( [genre_id] => 8 [genre_name] => Psychological ) [8] => Array (
    [genre_id] => 9 [genre_name] => Romance ) [9] => Array ( [genre_id] =>
    10 [genre_name] => Sci-fi ) [10] => Array ( [genre_id] => 11
    [genre_name] => Mystery ) [11] => Array ( [genre_id] => 12
    [genre_name] => Tragedy ) [12] => Array ( [genre_id] => 13
    [genre_name] => Short Story ) [13] => Array ( [genre_id] => 14
    [genre_name] => Satire ) ) )</p>
  </blockquote>
  
  <p>I've been looking at various questions regarding arrays from assoc to multidimensional and other stuff. Then i finally visited php manual for foreach loop and i finally was able to echo the array. The problem now is that although it echoes my array it also has an error for each, i can probably fix the error part by declaring it as a defined variable. My question is, is there any other better way? or any improvement on how i made my array to make it cleaner or easier to print?</p>
  
  <pre><code>foreach($tag_genre as $key =&gt; $value){
      foreach($value as $values){
          echo $values['tag_name'];
      }
  }
  </code></pre>
  
  <p>UPDATE: i changed the way i made the array.</p>
  
  <p>This is now my model:</p>
  
  <pre><code>public function get_tags(){
      $query = $this-&gt;db-&gt;get('tags')-&gt;result_array();
  
      foreach($query as $row){
      $result[$row['tag_id']] = $row['tag_name'];}
      return $result;
  }
  
  public function get_genre(){
      $query = $this-&gt;db-&gt;get('genre')-&gt;result_array();
  
      foreach($query as $row){
      $result[$row['genre_id']] = $row['genre_name'];}
      return $result;
  }
  </code></pre>
  
  <p>and my controller:</p>
  
  <pre><code>public function view_publish_story(){
  $data = array('tag' =&gt; $this-&gt;story_model-&gt;get_tags(), 'genre' =&gt; $this-&gt;story_model-&gt;get_genre(), 'title' =&gt; "New Story");
  
  $this-&gt;load-&gt;view('template/header',$data);
  $this-&gt;load-&gt;view('template/navbar');
  $this-&gt;load-&gt;view('pages/storypublish',$data);
  $this-&gt;load-&gt;view('template/footer');   
  }
  </code></pre>
  
  <p>and in my view:</p>
  
  <pre><code>&lt;tr&gt;
  &lt;div class="form-group"&gt;
      &lt;td&gt;&lt;label for="genre"&gt;Genre&lt;/label&gt;&lt;/td&gt;
      &lt;?php  
      foreach($genre as $genre_id =&gt; $genre_name){
      echo "&lt;td&gt;&lt;label class='checkbox-inline'&gt;&lt;input type='checkbox' value=''&gt;".$genre_name."&lt;/label&gt;&lt;td&gt;";
      }
      ?&gt;
  &lt;/div&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>My problem now is that, how do i fit all these into my table? I just ruined my table right now since i echoed them all in a single line. How do i do it so that it is printed in 3 columns?</p>