Professional Documents
Culture Documents
In PHP, two-dimensional arrays are arrays that contain arrays. You can think of the outer array as containing the rows and the inner arrays as containing the data cells in those rows. For example, a two-dimensional array called $rockBands could contain the names of the bands and some of the songs that they sing. Below is a grid that represents such a two-dimensional array.
The following code creates this two-dimensional array. The internal arrays are highlighted. Note that the header row is not included.
Syntax
1 2 3 4 5 6 7 $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') )
Syntax
1 2 3 4 5 6 7 8 9 10 11 12 13 <table border="1"> <?php foreach($rockBands as $rockBand) { echo "<tr>";
foreach($rockBand as $item) { echo "<td>$item</td>"; } echo "</tr>"; } ?> </table> The above code snippets are combined in the following example to output a rockBands table.