如何为从php数组创建的html表中的不同行设置不同的样式?

I'm struggling with creating a html table from php array, while applying different css classes on different rows according to the array.

I have this code in data.php:

<?php
$data = array(
  array('Title 1'=>'text11', 'Title 2'=>'text12'),
  array('Title 1'=>'text21', 'Title 2'=>'text22'),
  array('Title 1'=>'text31', 'Title 2'=>'text32'),
  array('Title 1'=>'text41', 'Title 2'=>'text42', 'special'=>'style1'),
  array('Title 1'=>'text51', 'Title 2'=>'text52', 'special'=>'style2'),
  );
?>

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:

<?php include('data.php'); ?>
<table>
  <thead>
    <tr>
      <th>Title 1</th>
      <th>Title 2</th>
    </tr>
  </thead>
  <tbody>
<?php foreach ($data as $key=>$row):
        if ($row == 'class1') {
          $class='class="style1"';
          } elseif ($row == 'class1') {
            $class='class="style2"';
          } else {
            $class='';
          }?>
    <tr <?php echo $class ?>>
      <td><?php echo implode('</td><td>', $row); ?></td>
    </tr>
<?php endforeach; ?>
  </tbody>
</table>

And this is the desired output:

<table>
  <thead>
    <tr>
      <th>Title 1</th>
      <th>Title 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>text11</td><td>text12</td>
    </tr>
    <tr>
      <td>text21</td><td>text22</td>
    </tr>
    <tr>
      <td>text31</td><td>text32</td>
    </tr>
    <tr class="style1">
      <td>text41</td><td>text42</td>
    </tr>
    <tr class="style2">
      <td>text51</td><td>text52</td>
    </tr>
  </tbody>
</table>

Your problem is this line:

<?php foreach ($data as $key=>$row):

You're forgetting that you're looping over a multidimensional array (i.e. an array of arrays) so that $row is an array here. On the next line:

if ($row == 'class1')

You're looking for a comparison between a string and $row which is an array. This will never work! As Daniel points out in his answer, you need to look at the contents of the array.

Don't forget you'll need to remove the special element from the array before displaying it. Personally, I'd condense the code a bit, though mixing PHP and HTML like this is never a good idea.

<?php include('data.php'); ?>
<table>
  <thead>
    <tr>
      <th>Title 1</th>
      <th>Title 2</th>
    </tr>
  </thead>
  <tbody>
<?php foreach ($data as $row): $class = $row["special"] ?? ""; unset($row["special"]);?>
    <tr class="<?=$class?>">
      <td><?=implode("</td><td>", $row)?></td>
    </tr>
<?php endforeach; ?>
  </tbody>
</table>

If I understand your question correctly, this should do the job.

The result is as you desire, see: Online PHP shell

<table>
      <thead>
        <tr>
          <th>Title 1</th>
          <th>Title 2</th>
        </tr>
      </thead>
      <tbody>
    <?php foreach ($data as $key=>$row):
            if (isset($row["special"])) {
              $class = " class='" . $row["special"]. "'";    
              unset($row["special"]);
              } else {
                $class='';
              }?>
        <tr<?php echo $class ?>>
          <td><?php echo implode('</td><td>', $row); ?></td>
        </tr>
    <?php endforeach; ?>
      </tbody>
    </table>

I'm not sure I understood everything but you can try this

<?php foreach ($data as $key => $row):
    $class = '';
    if(isset($row['special'])){
        $class = 'class="'.$row['special'].'"';
        unset($row['special']);
    }
 ?>
<tr <?php echo $class ?>>
    <td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>