title: Display Array as a Table
body: <p>I am trying to print a table using PHP/HTML. Data stored inside array like this:</p>
  
  <pre><code>Array ( [id] =&gt; 1 [first_name] =&gt; mike [last_name] =&gt; lastname )
  </code></pre>
  
  <p>My code is as follow. It runs and there are no error however the output is not as expected. Here is the PHP/HTML code:</p>
  
  <pre><code>&lt;table&gt;
  
  &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;th&gt;2&lt;/th&gt;
      &lt;th&gt;3&lt;/th&gt;
  &lt;/tr&gt;
  
  &lt;?php foreach ($res as $item): ?&gt;
  &lt;tr&gt;
       &lt;td&gt;&lt;?php echo $item['id'] ?&gt;&lt;/td&gt;
       &lt;td&gt;&lt;?php echo $item['first_name'] ?&gt;&lt;/td&gt;
       &lt;td&gt;&lt;?php echo $item['last_name'] ?&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php endforeach; ?&gt;
  
  &lt;/table&gt;
  </code></pre>
  
  <p>The result I get is the first character of the items:</p>
  
  <pre><code>1       2      3
  1       1      1
  m       m      m
  l       l      l
  </code></pre>
  
  <p>Not sure what I am doing wrong? I would really appreciate an explanation.</p>
  
  <p>UPDATE:</p>
  
  <p>PHP CODE that has no errors:</p>
  
  <pre><code>&lt;?php 
      foreach ($result as $row) 
      { 
          echo '&lt;tr&gt;';
          echo '&lt;td&gt;' . $row['id'] . '&lt;/td&gt;';
          echo '&lt;td&gt;' . $row['first_name'] . '&lt;/td&gt;';
          echo '&lt;td&gt;' . $row['last_name'] . '&lt;/td&gt;';
          echo '&lt;/tr&gt;';
      }
  ?&gt;
  </code></pre>
  
  <p>And this is my array with only one "row" in it:</p>
  
  <p>Output of $result variable using print_r($result) wrapped with &lt; PRE > tags</p>
  
  <pre><code>Array
  (
      [id] =&gt; 3
      [first_name] =&gt; Jim
      [last_name] =&gt; Dude
  )
  </code></pre>
  
  <p>Here is the result I am getting:</p>
  
  <p>Actual Table result:</p>
  
  <pre><code>ID    First Name  Last Name
  3     3           3
  J     J           J
  D     D           D
  </code></pre>
  
  <p>However if I have 0 in array or more than 1 (meaning 2 or more) it works perfectly. IT JUST DOES NOT WORK WHEN I HAVE ONLY ONE "ROW" OF ELEMENTS INSIDE ARRAY. For example this array, works perfectly:</p>
  
  <pre><code>  Array
      (
      [0] =&gt; Array
          (
              [id] =&gt; 3
              [first_name] =&gt; Jim
              [last_name] =&gt; Dude
          )
  
      [1] =&gt; Array
          (
              [id] =&gt; 4
              [first_name] =&gt; John
              [last_name] =&gt; Dude2
          )
      )
  </code></pre>
  
  <p>I get this result:</p>
  
  <pre><code>ID      First Name    Last Name
  3       Jim         Dude
  4       John            Dude2
  </code></pre>
  
  <p>I am not really sure what I am doing wrong. The idea is not knowing how many items is in a Table read it into <code>$result</code> variable and then using this variable print all elements inside HTML table. Table could contain O elements, 1 row of elements, 1 or more rows of elements.</p>