HTMLのテーブル(表)の内容を、PHPの2次元配列で指定する方法。
サンプル
PHPコード
<?php
$array = array(
array( "1-1", "1-2", "1-3" ),
array( "2-1", "2-2", "2-3" ),
array( "3-1", "3-2", "3-3" ),
);
$sampleTable = "<table>\n";
for ( $indexA = 0; $indexA < count( $array ); $indexA++ ) {
$sampleTable .= "\t<tr>\n";
for ( $indexB = 0; $indexB < count( $array[$indexA] ); $indexB++ ) {
$sampleTable .= "\t\t<td>{$array[$indexA][$indexB]}</td>\n";
}
$sampleTable .= "\t</tr>\n";
}
$sampleTable .= "</table>\n";
echo "{$sampleTable}";
?>
$array = array(
array( "1-1", "1-2", "1-3" ),
array( "2-1", "2-2", "2-3" ),
array( "3-1", "3-2", "3-3" ),
);
$sampleTable = "<table>\n";
for ( $indexA = 0; $indexA < count( $array ); $indexA++ ) {
$sampleTable .= "\t<tr>\n";
for ( $indexB = 0; $indexB < count( $array[$indexA] ); $indexB++ ) {
$sampleTable .= "\t\t<td>{$array[$indexA][$indexB]}</td>\n";
}
$sampleTable .= "\t</tr>\n";
}
$sampleTable .= "</table>\n";
echo "{$sampleTable}";
?>
↓↓↓出力結果↓↓↓
出力結果
HTMLコード
<table>
<tr>
<td>1-1</td>
<td>1-2</td>
<td>1-3</td>
</tr>
<tr>
<td>2-1</td>
<td>2-2</td>
<td>2-3</td>
</tr>
<tr>
<td>3-1</td>
<td>3-2</td>
<td>3-3</td>
</tr>
</table>
<tr>
<td>1-1</td>
<td>1-2</td>
<td>1-3</td>
</tr>
<tr>
<td>2-1</td>
<td>2-2</td>
<td>2-3</td>
</tr>
<tr>
<td>3-1</td>
<td>3-2</td>
<td>3-3</td>
</tr>
</table>
ブラウザ表示例
1-1 | 1-2 | 1-3 |
2-1 | 2-2 | 2-3 |
3-1 | 3-2 | 3-3 |