PHP Foreach数组到表格显示

时间:2014-11-25 11:21:20

标签: php arrays foreach

我有一个数组$day,并希望以表格形式显示此数组(我正在使用CI)

Array ( 
[START_EXECUTION] => 
    Array ( 
        [0] => 27-OCT-14 
        [1] => 28-OCT-14 
        [2] => 29-OCT-14 
          ) 
[NUM_OF_POPULATION] => 
    Array ( 
        [0] => 6171 
        [1] => 6990 
        [2] => 6882 
          ) 
[SUM_AMOUNT] => 
        Array ( 
        [0] => 361154716.01 
        [1] => 409210099.77 
        [2] => 407191552.71 
          ) 
)

以下是我在视图中使用的代码:

<?php
if(count($day)>0){
    print_r($day);
    foreach($day as $index => $dt1_element){    
?>        
    <table class='table'>
        <tr>
        <td><?= $index ?></td>
        </tr>        
<?php
    foreach($dt1_element as $row){        
?>
        </tr>
        <td><?= $row ?></td>
<?php
    }
?>
        </tr>
<?php
    }
?>
    </table>
<?php
    }
?

但我得到的是这样的:

START_EXECUTION
27-OCT-14
28-OCT-14
29-OCT-14

NUM_OF_POPULATION
6171
6990
6882

SUM_AMOUNT
361154716.01
409210099.77
407191552.71

结果应为:

START_EXECUTION   NUM_OF_POPULATION   SUM_AMOUNT
27-OCT-14         6171                361154716.01
28-OCT-14         6990                409210099.77
29-OCT-14         6882                407191552.71

请告诉我正确的foreach以获得理想的结果。谢谢

6 个答案:

答案 0 :(得分:3)

试试这个:

echo '<table>';

$cols = array_keys($day);
echo '<tr>';
foreach ($cols as $col) echo '<th>' . $col . '</th>';
echo '</tr>';

foreach ($day[$cols[0]] as $i => $null) {
    echo '<tr>';
    foreach ($cols as $col) echo '<td>' . $day[$col][$i] . '</td>';
    echo '</tr>';
}

echo '</table>';

enter image description here

demo

答案 1 :(得分:0)

您不必要地关闭<tr>

您需要的只是一个单独行上的子金额。

更正后的代码:

<?php
if(count($day)>0){
    print_r($day);
    foreach($day as $index => $dt1_element){    
?>        
  <table class='table'>
        <tr>
        <td><?php echo $index;?></td>
        </tr>        
<?php
$tds = array();
    foreach($dt1_element as $row){        
                $tds[] = '<td>'.$row.'</td>';
?>
<?php
    }
    echo "<tr>". impldoe(' ', $tds) ."</tr>";
    ?>

    <?php
   }
?>
    </table>
<?php
    }
?>

答案 2 :(得分:0)

如果你使用PHP&gt; = 5.5而不是

echo "<table>";

$cols = array_keys($day);
echo "<tr><th>";
echo implode('</th><th>', $cols);
echo "</th></tr>";

for ($i = 0, $num = count($cols); $i < $num; ++$i)
{
    echo "<tr><td>";
    echo implode('</td><td>', array_column($day, $i));
    echo "</td></tr>";
}

echo "</table>";

答案 3 :(得分:0)

这里工作正常

print "<table><tr><th>START_EXECUTION |</th><th>NUM_OF_POPULATION |</th><th>SUM_AMOUNT |</th></tr>";
//$index=0;
foreach($vals['START_EXECUTION'] as $index=>$values){

    echo "<tr><td>$values</td><td>".$vals['NUM_OF_POPULATION'][$index]."</td><td>".$vals['SUM_AMOUNT'][$index]."</td></tr>";
    //$index++;
} print '</table>';

for demo here

答案 4 :(得分:0)

我的回答受到了&#34;两步查看&#34;的启发。我为视图构建逻辑数据的模式,以避免在视图中出现任何不必要的信息(例如,如果我可以帮助它,我不会在视图中拥有数组索引):

<?php
    $arr = array(
        'START_EXECUTION'=>array(
            '27-OCT-14',
            '28-OCT-14',
            '29-OCT-14',
        ),
        'NUM_OF_POPULATION'=>array(
            6171,
            6990,
            6882,
        ),
        'SUM_AMOUNT'=>array(
            361154716.01,
            409210099.77, 
            407191552.71,
        ),
    );

    $headers = array_keys($arr);

    $body = array_map(function($a, $b, $c) { return array($a, $b, $c); },
        $arr['START_EXECUTION'],
        $arr['NUM_OF_POPULATION'],
        $arr['SUM_AMOUNT']
    );

    $table = array(
        'headers'=>$headers,
        'body'=>$body,
    );
?>

$table将以独立于视图的方式包含表的逻辑结构。

我们可以创建一个简单的小部件,将逻辑数据转换为html表,如下所示:

 <table class="table">
    <thead>
    <tr>
        <?php foreach($table['headers'] as $header): ?>
            <th><?php echo $header; ?></th>
        <?php endforeach; ?>
     </tr>
     </thead>
     <tbody>
     <?php foreach ($table['body'] as $row): ?>
     <tr>
     <?php foreach ($row as $col): ?>
          <td><?php echo $col; ?></td>
     <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

只要您保持相同的逻辑结构,html代码段就可以放在&#34;小部件中。这就是表格渲染器的开始。

注意这只是一个原型,并不是一个全面的解决方案。

答案 5 :(得分:0)

$data = array($FIELDS);//field value like name,email,password etc.
foreach($data as $row)
{
    echo "<tr>";
    foreach($row as $column){
    echo "<th>".$column."</th>";
}
echo "</tr>";
}

你可以创建字段数组并在数组中添加字段数据试试这个......