2017-10-11 125 views
0

我有兩個數組:打印縱向和橫向的頭

$array1 = array(1,2,3,4,7,6); 
$array2 = array(2,3,1,0,5); 

然後我試圖創建printTable功能打印乘法表數組:

function printTable($array1, $array2) { 
    echo "<table border=1px>"; 
    foreach ($array2 as $vert_head) { 
    echo "<tr>"; 
    foreach ($array1 as $hor_head) { 
     echo "<td>". ($vert_head * $hor_head) ."</td>"; 
    } 
    echo "</tr>"; 
    } 
    echo "</table>"; 
} 

,然後打印表格:

$data = printTable($array1, $array2); 

它工作正常表是打印。但是,我怎樣才能從數組中添加水平和垂直標題?

那我的表怎麼看現在(只是答案):

2 | 4 | 6 | 8 | 14 | 12 | 
3 | 6 | 9 | 12 | 21 | 18 | 
1 | 2 | 3 | 4 | 7 | 6 | 
0 | 0 | 0 | 0 | 0 | 0 | 
5 | 10 | 15 | 20 | 35 | 30 | 

如何添加標題:

| | 1 | 2 | 3 | 4 | 7 | 6 | 
+---+---+----+----+----+----+----+ 
| 2 | 2 | 4 | 6 | 8 | 14 | 12 | 
| 3 | 3 | 6 | 9 | 12 | 21 | 18 | 
| 1 | 1 | 2 | 3 | 4 | 7 | 6 | 
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 
| 5 | 5 | 10 | 15 | 20 | 35 | 30 | 

回答

0

我已經包含了代替修改代碼的註釋。它使用和額外的foreach循環來打印標題。

<?php 

$array1 = array(1,2,3,4,7,6); 
$array2 = array(2,3,1,0,5); 

function printTable($array1, $array2) { 
    echo "<table border=1px>"; 

    /* In our table first cell is empty. So put a blank cell here. */ 
    echo '<tr><td></td>' ; 

    /* Then print all headers in following cells */ 
    foreach($array1 as $head) { 
    echo '<th>' . $head . '</th>' ; 
    } 
    /* close the first row */ 
    echo '</tr>'; 

    foreach ($array2 as $vert_head) { 
    echo "<tr>"; 

    /* Before printing each row, print the left heading */ 
    echo '<td>' . $vert_head . '</td>' ; 

    foreach ($array1 as $hor_head) { 
     echo "<td>". ($vert_head * $hor_head) ."</td>"; 
    } 
    echo "</tr>"; 
    } 
    echo "</table>"; 
} 
+0

最簡單的方法雖然這個代碼片斷可以解決這個問題,[包括說明](// meta.stackexchange.com/q/114762)確實有助於提高質量您的帖子。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 –

+0

@CodyGray修改了答案,並感謝您的建議。我確實犯了錯誤,我確實試圖改善我在各個方面的問題和答案。 – tan

2

嘗試以下操作:

<?php 
$array1 = array(1,2,3,4,7,6); 
$array2 = array(2,3,1,0,5); 
function printTable($array1, $array2) { 
    echo "<table border=1px>"; 
    echo "<thead><tr><td></td>"; 
    echo "<th>" . implode("</th><th>", $array1) . "</th>"; 
    echo "</tr></thead><tbody>"; 
    foreach ($array2 as $vert_head) { 
    echo "<tr>"; 
    foreach ($array1 as $hor_head) { 
     echo "<td>". ($vert_head * $hor_head) ."</td>"; 
    } 
    echo "</tr>"; 
    } 
    echo "</tbody></table>"; 
} 

printTable($array1, $array2); 

它使用implode()$array1轉換爲表標題

1

你也可以試試這種方式。這是與您現有的代碼

$array1 = array(1,2,3,4,7,6); 
$array2 = array(2,3,1,0,5); 
function printTable($array1, $array2) { 
    echo "<table border='1px' style='text-align:right'>"; 
    echo '<tr><td></td>' ; 
    foreach($array1 as $head) { 
    echo '<td>' . $head . '</td>' ; 
    } 
    echo '</tr>'; 
    foreach ($array2 as $vert_head) { 
    echo "<tr>"; 
    echo '<td>' . $vert_head . '</td>' ; 
    foreach ($array1 as $hor_head) { 
     echo "<td>". ($vert_head * $hor_head) ."</td>"; 
    } 
    echo "</tr>"; 
    } 
    echo "</table>"; 
} 
$data = printTable($array1, $array2);