1

I have an array of n elements, of the form:

array (
    array ("FOO", "BAR"),
    array ("FOO", "BAR"),
    array ("FOO", "BAR")...
)

I would like to loop over the array and display them on an HTML table.

2 Answers 2

1
<? $bigArray = array( array("foo", "bar"), array("foo", "bar"), array("foo", "bar") ); ?>
<table>
<? foreach($bigArray as $a) { ?>
    <tr><? for($j=0; $j <= 5; ++$j) { ?><td><?= $a[$j] ?></td><? } ?></tr>
<? } ?>
</table>

The advantage of this approach is that you can prototype with your favorite html editor and plug the commands in. Note that this only works when your server supports short_tags.

Sign up to request clarification or add additional context in comments.

1 Comment

you can replace the 5 with your bound (i.e. $j < $max_elements)
1

Try a foreach loop.

Foreach:

<?
$bigArray = array( array("foo", "bar"), array("foo", "bar"), array("foo", "bar") );
?>
<table>
<?
    foreach($bigArray as $a)
    {
        echo "<tr><td>".$a[0]."</td><td>".$a[1]."</td></tr>";
    }
?>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.