Foreach源PHP数组表显示 [英] PHP Foreach array to table display

查看:113
本文介绍了Foreach源PHP数组表显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组 $日和希望显示在表格的形式这阵(我与CI工作)

I have an array $day and want to display this array in table form (I'm working with 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 
          ) 
)

下面是我的code,我在视图中使用:

Here is my code that I use in view :

<?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
    }
?

但我得到的是这样的:

But what I get is like this :

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来获得期望的结果。谢谢

Kindly show me the correct foreach to get the desired result. Thank you

推荐答案

试试这个:

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>';

<大骨节病> 演示

这篇关于Foreach源PHP数组表显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆