为数组项分配2个值

时间:2014-04-28 09:13:59

标签: php arrays

我有这个PHP数组:

$statuses = array('Customer Reply', 'Needs Action', 'Open');

然后我运行此代码:

foreach($statuses as $status) {
    echo '<tr>
    <td colspan="8" bgcolor="#F36F25"><strong><font color="#FFFFFF">'.$status.' ('.count($records).')</font></strong></td>
    </tr>';
}

但我希望能够为每个$statuses分配不同的颜色(背景)

3 个答案:

答案 0 :(得分:1)

这就是你想要的......

But i want to be able to assign a different colour (for the background) for each

$statuses = array('color_code'=>'Customer Reply', 'color_code'=>'Needs Action', 'color_code'=>'Open');
foreach($statuses as $color=>$status) {
    echo '<tr>
    <td colspan="8" bgcolor="'. $color .'"><strong><font color="#FFFFFF">'.$status.'</font></strong></td>
    </tr>\n';
}

答案 1 :(得分:0)

$statuses = array('Customer Reply' => '#FFF', 'Needs Action' => '#000', 'Open' => '#F36F25');

foreach($statuses as $status => $color) { ?>
<tr>
  <td colspan ="8" bgcolor="<?php echo $color; ?>"><font color="#FFFFFF"><?php echo $status; echo count($records); ?></font></strong></td>
</tr>
<?php
}

答案 2 :(得分:-1)

取决于您的需求。 Modulo就是你想要的。

$statuses = array('Customer Reply', 'Needs Action', 'Open');

foreach($statuses as $key => $status) {
  $color = 'white';
  if($key%2) {
    $color = 'black';
  }
    echo '<tr>
    <td colspan="8" bgcolor="'. $color .'"><strong><font color="#FFFFFF">'.$status.'</font></strong></td>
    </tr>\n';
}