aquastream Posted February 7, 2010 Share Posted February 7, 2010 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? Link to comment https://forums.phpfreaks.com/topic/191228-place-foreach-items-in-columns-instead-of-rows/ Share on other sites More sharing options...
sasa Posted February 7, 2010 Share Posted February 7, 2010 try <html> <head> <title>Two-dimensional Arrays</title> </head> <body> <h1>Two-Dimensional Arrays</h1> <?php $Rockbands = array( array('Rockband', 'Song 1', 'Song 2', 'Song 3'), 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($Rockbands as $Rockband) { echo '<tr>'; foreach($Rockband as $item) { echo "<td>$item</td>"; } echo '</tr>'; } ?> </table> <hr /> <?php $Rockbands1= array(); foreach ($Rockbands as $i => $Rockband){ foreach ($Rockband as $j => $value) $Rockbands1[$j][$i] = $value; } echo '<table border="1">'; foreach($Rockbands1 as $Rockband) { echo '<tr>'; foreach($Rockband as $item) { echo "<td>$item</td>"; } echo '</tr>'; } ?> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/191228-place-foreach-items-in-columns-instead-of-rows/#findComment-1008282 Share on other sites More sharing options...
aquastream Posted February 7, 2010 Author Share Posted February 7, 2010 Thanks sasa for the reply. I already tried your first example before posting. While it places the contents into the table properly, I was looking to specifically set the first row as a table header so that it displays and bold. Your second example however was useful as it gives me a few things to keep in mind to change rows of an array into columns. Link to comment https://forums.phpfreaks.com/topic/191228-place-foreach-items-in-columns-instead-of-rows/#findComment-1008310 Share on other sites More sharing options...
wildteen88 Posted February 7, 2010 Share Posted February 7, 2010 If you're using sasa's code above try ... <?php $Rockbands1= array(); foreach ($Rockbands as $i => $Rockband){ foreach ($Rockband as $j => $value) $Rockbands1[$j][$i] = $value; } echo '<table border="1">'; foreach($Rockbands1 as $Rockband) { echo '<tr>'; foreach($Rockband as $item) { echo "<td>$item</td>"; } echo '</tr>'; } ?> ... Change foreach($Rockbands1 as $Rockband) { echo '<tr>'; foreach($Rockband as $item) { echo "<td>$item</td>"; } echo '</tr>'; } To foreach($Rockbands1 as $key => $Rockband) { $tag = ($key === 0) ? 'th' : 'td'; echo '<tr>'; foreach($Rockband as $item) { echo "<$tag>$item</$tag>"; } echo '</tr>'; } Link to comment https://forums.phpfreaks.com/topic/191228-place-foreach-items-in-columns-instead-of-rows/#findComment-1008323 Share on other sites More sharing options...
aquastream Posted February 8, 2010 Author Share Posted February 8, 2010 Alright! Thanks wildteen88. That's what I wanted. I'll now take a look at that code and try to learn from it. Link to comment https://forums.phpfreaks.com/topic/191228-place-foreach-items-in-columns-instead-of-rows/#findComment-1008622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.