So I went to http://www.learnphp-tutorial.com/Arrays.cfm and saw their code sample on two-dimensional arrays. Here's the code:
<html>
<head>
<title>Two-dimensional Arrays</title>
</head>
<body>
<h1>Two-Dimensional Arrays</h1>
<?php
$Rockbands = array(
array('Beatles','Love Me Do', 'Hey Jude','Helter Skelter'),
array('Rolling Stones','Waiting on a Friend','Angie','Yesterday\'s Papers'),
array('Eagles','Life in the Fast Lane','Hotel California','Best of My Love')
);
?>
<table border="1">
<tr>
<th>Rockband</th>
<th>Song 1</th>
<th>Song 2</th>
<th>Song 3</th>
</tr>
<?php
foreach($Rockbands as $Rockband)
{
echo '<tr>';
foreach($Rockband as $item)
{
echo "<td>$item</td>";
}
echo '</tr>';
}
?>
</table>
</body>
</html>
Seeing the way that they used HTML in order to create the table headers, I thought I could do better than that and instead list the header names in an array, use a foreach function and then echo the results back. However, this isn't the result I'm getting as you can see below.
<html>
<head>
<title>Array Test</title>
</head>
<body>
<?php
$Rockband_Header = array("Rockband","Song 1","Song 2","Song 3");
$Rockbands = array(
array("Beatles","Love Me Do","Hey Jude","Helter Skelter"),
array("Rolling Stones","Waiting on a Friend","Angie","Yesterday's Papers"),
array("Eagles","Life in the Fast Lane","Hotel California","Best of My Love")
);
?>
<table border="1">
<?php
foreach ($Rockband_Header as $Header) {
echo "<tr>";
foreach ($Rockbands as $Rockband) {
echo "<th>$Header</th>";
foreach ($Rockband as $Items)
echo "<td>$Items</td>";
}
echo "</tr>";
}
?>
</table>
</body>
</html>
On looking back at the code, I noticed that instead of the <th> for $Header being located inside a single <tr>, somehow the code is resulting in a new <tr> being produced for each of the <th>'s being echoed. So how do I get the <th>'s to be located inside a single <tr>, and therefore result in the same appearance as the original code above?