爆炸表中的数组

时间:2014-04-26 13:18:19

标签: php arrays

我有这种类型的数组:

Array (
  [0] => Array (
    [a] => BANANA
    [b] => 975
    [c] => 57
  )
  [1] => Array (
    [a] => COCONUT
    [b] => 7
    [c] => 7
  )
)

对于每个我希望有这样的值:

<tr>
    <td colspan="4" class="text-right no-border"><strong>BANANA - 975 %</strong></td>
    <td>57</td>
    <td>&nbsp;</td>
</tr>
<tr>
    <td colspan="4" class="text-right no-border"><strong>COCONUT - 7 %</strong></td>
    <td>7</td>
    <td>&nbsp;</td>
</tr>

如何做到这一点。

感谢您的帮助。

4 个答案:

答案 0 :(得分:1)

使用 heredoc 语法可以完成一百万个解决方案。

代码:

$my_html_document = "";
foreach($arr as $fruit=>$data) {
  $my_html_document .= <<<HTML
<tr>
  <td colspan="4" class="text-right no-border"><strong>{$data['a']} - {$data['b']} %</strong>      </td>
  <td>{$data['c']}</td>
  <td>&nbsp;</td>
</tr>\n
HTML;
}

echo $my_html_document;

输出:

<tr>
  <td colspan="4" class="text-right no-border"><strong>BANANA - 975 %</strong>    </td>
  <td>57</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td colspan="4" class="text-right no-border"><strong>COCONUT - 7 %</strong>    </td>
  <td>7</td>
  <td>&nbsp;</td>
</tr>

答案 1 :(得分:0)

<?php foreach($array as $value): ?>
<tr>
    <td colspan="4" class="text-right no-border"><strong><?php echo $value['a'] . ' - ' . $value['b'] . '%' ?></strong></td>
    <td><?php echo $value['c'] ?></td>
    <td>&nbsp;</td>
</tr>
<?php endforeach; ?>

答案 2 :(得分:0)

这是你的答案

 $items=Array (
  [BANANA] => Array (
     [a] => BANANA
     [b] => 975
     [c] => 57
)
  [COCONUT] => Array (
    [a] => COCONUT
    [b] => 7
    [c] => 7
)
)
 $str='';
 foreach($items as $item){
      $str=$str."<tr>
        <td colspan='4' class='text-right no-border'><strong>".$item["a"]." - ".$item["b"]." %</strong>  </td>
        <td>".$item["c"]."</td>
       <td>&nbsp;</td>
   </tr>";
 }
 echo $str;

答案 3 :(得分:0)

请试试这个......

<?php

    $array = array(
                'banana' => array(
                            'a' => 'Banana',
                            'b' => 975,
                            'c' => 57,
                    ),
                'coconut' => array(
                            'a' => 'coconut',
                            'b' => 7,
                            'c' => 7,
                    ),
            );




    foreach($array as $value) { 
    ?>
    <tr>
        <td colspan="4" class="text-right no-border"><strong><?php echo $value['a'] . ' - ' . $value['b'] . '%' ?></strong></td>
        <td><?php echo $value['c'] ?></td>
        <td>&nbsp;</td>
    </tr>
    <?php } ?>