2014-11-25 35 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>"; 
} 

你可以在數組中創建數組並添加字段數據試試這個......