title: How to set different styles for different rows in html table created from php array?
body: <p>I'm struggling with creating a html table from php array, while applying different css classes on different rows according to the array.</p>
  
  <p>I have this code in <em>data.php</em>:</p>
  
  <pre><code>&lt;?php
  $data = array(
    array('Title 1'=&gt;'text11', 'Title 2'=&gt;'text12'),
    array('Title 1'=&gt;'text21', 'Title 2'=&gt;'text22'),
    array('Title 1'=&gt;'text31', 'Title 2'=&gt;'text32'),
    array('Title 1'=&gt;'text41', 'Title 2'=&gt;'text42', 'special'=&gt;'style1'),
    array('Title 1'=&gt;'text51', 'Title 2'=&gt;'text52', 'special'=&gt;'style2'),
    );
  ?&gt;
  </code></pre>
  
  <p>I want to create a html table from this array, and if the array contains 'special'=>'style', it would set that style to that particular row. This is my code so far:</p>
  
  <pre><code>&lt;?php include('data.php'); ?&gt;
  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Title 1&lt;/th&gt;
        &lt;th&gt;Title 2&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
  &lt;?php foreach ($data as $key=&gt;$row):
          if ($row == 'class1') {
            $class='class="style1"';
            } elseif ($row == 'class1') {
              $class='class="style2"';
            } else {
              $class='';
            }?&gt;
      &lt;tr &lt;?php echo $class ?&gt;&gt;
        &lt;td&gt;&lt;?php echo implode('&lt;/td&gt;&lt;td&gt;', $row); ?&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;?php endforeach; ?&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>And this is the desired output:</p>
  
  <pre><code>&lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Title 1&lt;/th&gt;
        &lt;th&gt;Title 2&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;text11&lt;/td&gt;&lt;td&gt;text12&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;text21&lt;/td&gt;&lt;td&gt;text22&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;text31&lt;/td&gt;&lt;td&gt;text32&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr class="style1"&gt;
        &lt;td&gt;text41&lt;/td&gt;&lt;td&gt;text42&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr class="style2"&gt;
        &lt;td&gt;text51&lt;/td&gt;&lt;td&gt;text52&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>