title: To make data into table format before exporting to Excel
body: <p>I use this function below to process the array data I retrieve from my db, then export this data for Excel. </p>
  
  <pre><code># feed the final items to our formatting function...
  $contents = get_excel_data($items);
  
  function get_excel_data($items){
  
      # set the variable
      $output = null;
  
      # check if the data is an items and is not empty
      if (is_array($items)  &amp;&amp; !empty($items))
      {
          # start the row at 0
          $row = 0;
  
          # loop the items
          # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice.
          # foreach(array_values($items) as $item)
          foreach($items as $item)
          {
              if (is_array($item) &amp;&amp; !empty($item))
              {
                  if ($row == 0)
                  {
                      # write the column headers
                      $output = implode("\t",array_keys($item));
                      $output .= "\n";
                  }
                      # create a line of values for this row...
                      $output .= implode("\t",array_values($item));
                      $output .= "\n";
  
                      # increment the row so we don't create headers all over again
                      $row++;
              }
          }
      }
  
      # return the result
      return $output;
  }
  </code></pre>
  
  <p>It processes the output like this below,</p>
  
  <pre><code>cat_id  cat_name    cat_order   cat_hide    cat_important   cat_created cat_updated
  1   Primary image   1   0   1   0000-00-00 00:00:00 2011-01-17 17:26:51
  2   Secondary image 2   0   1   0000-00-00 00:00:00 2011-01-17 17:27:01
  3   Tertiary image  3   0   1   0000-00-00 00:00:00 2010-10-08 20:03:56
  4   Quartary image  4   0   1   0000-00-00 00:00:00 2010-10-08 20:03:56
  </code></pre>
  
  <p>but ideally I want to wrap the output in a table like this,</p>
  
  <pre><code> &lt;table border="1"&gt;
  &lt;tr&gt;
  &lt;th&gt;cat_id&lt;/th&gt; 
  &lt;th&gt;cat_name&lt;/th&gt;   
  &lt;th&gt;cat_order&lt;/th&gt;  
  &lt;th&gt;cat_hide&lt;/th&gt;   
  &lt;th&gt;cat_important&lt;/th&gt;  
  &lt;th&gt;cat_created&lt;/th&gt;    
  &lt;th&gt;cat_updated&lt;/th&gt;
  &lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td&gt;1&lt;/td&gt;  
  &lt;td&gt;Primary image&lt;/td&gt;  
  &lt;td&gt;1&lt;/td&gt;  
  &lt;td&gt;0&lt;/td&gt;  
  &lt;td&gt;1&lt;/td&gt;  
  &lt;td&gt;0000-00-00 00:00:00&lt;/td&gt;    
  &lt;td&gt;2011-01-17 17:26:51&lt;/td&gt;
  &lt;/tr&gt;
  
  &lt;td&gt;2&lt;/td&gt;  
  &lt;td&gt;Secondary image&lt;/td&gt;    
  &lt;td&gt;2&lt;/td&gt;  
  &lt;td&gt;0&lt;/td&gt;  
  &lt;td&gt;1&lt;/td&gt;  
  &lt;td&gt;0000-00-00 00:00:00&lt;/td&gt;    
  &lt;td&gt;2011-01-17 17:26:51&lt;/td&gt;
  &lt;/tr&gt;
  ...
  &lt;/table&gt;
  </code></pre>
  
  <p>Any ideas what I should add into the original code to output the table above?</p>
  
  <p>I tried with my version of modification below but it fails!</p>
  
  <pre><code>function get_excel_data($items){
  
      # set the variable
      $output = null;
  
      # check if the data is an items and is not empty
      if (is_array($items)  &amp;&amp; !empty($items))
      {
          # start the row at 0
          $row = 0;
  
          # loop the items
          # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice.
          # foreach(array_values($items) as $item)
          foreach($items as $key =&gt; $value )
          {
  
                  if ($row == 0)
                  {   
                      /*
                      # write the column headers
                      $output = implode("\t",array_keys($item));
                      $output .= "\n";
                      */
  
                      $output = '&lt;table border="1"&gt;&lt;tr&gt;';
                      $output .= '&lt;th&gt;'.$key.'&lt;/th&gt;';
                      $output .= '&lt;/tr&gt;';
                  }
                      /*
                      # create a line of values for this row...
                      $output .= implode("\t",array_values($item));
                      $output .= "\n";
                      */
  
                      $output .= '&lt;tr&gt;';
                      $output .= '&lt;td&gt;'.$value.'&lt;/td&gt;';
  
                      $output .= '&lt;/tr&gt;';
  
  
                  if ($row == 0)
                  {
                      $output .=  '&lt;/table&gt;';
                  }
  
                      # increment the row so we don't create headers all over again
                      $row++;
  
          }
      }
  
      # return the result
      return $output;
  }
  </code></pre>
  
  <p>Thanks.</p>