2014-11-25 256 views
3

我有一个数组$day并希望显示这个数组以表格形式(我与CI工作)PHP的foreach数组表显示

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以获得所需的结果。谢谢

回答

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

+0

优秀的答案。 – Pupil 2014-11-25 11:36:21

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

谢谢你的回答,但我仍然得到相同的结果 – 2014-11-25 11:28:55

+0

回答更新,它应该现在工作。 – Pupil 2014-11-25 11:31:16

0

如果您使用PHP> = 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>"; 
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>'; 

demo here

0

我的响应是由在那里我建立逻辑数据的“两步View”模式激发了视图,以避免任何不必要的信息是在视图中(例如,如果我可以提供帮助,我不是拥有视图中数组索引的粉丝):

<?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代码片段可以放在里面“小部件“,这是表格渲染器的开始。

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

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>"; 
} 

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