0
<?php
    $array = array(array("aaa1", "aaa2", "aaa3", "aaa4"),
                   array("bbb1", "bbb2", "bbb3", "bbb4"),
                   array("ccc1", "ccc2", "ccc3", "ccc4"), 
                   array("ddd1", "ddd2", "ddd3", "ddd4"));
    ?>
    <table border="2">
    <?php
    foreach($array as $arr): ?>
        <tr>
   <?php     foreach($arr as $a):
            echo "<td>" . $a . "</td>";
        endforeach; ?>
        </tr>
  <?php  endforeach;
    ?>
    </table>

这告诉我:

aaa1 aaa2 aaa3 aaa4
bbb1 bbb2 bbb3 bbb4
ccc1 ccc2 ccc3 ccc4
ddd1 ddd2 ddd3 ddd4

我想收到:

aaa1 bbb1 ccc1 ddd1
aaa2 bbb2 ccc2 ddd2
aaa3 bbb3 ccc3 ddd3
aaa4 bbb4 ccc4 ddd4

但我必须使用 FOREACH,而不是 FOR。如果这更容易,我可以使用 DIV,但是如何使用?

我怎样才能做到?

4

2 回答 2

3
foreach(array_keys($array[0]) as $key) {
    echo "<tr>";
    foreach($array as $arr) {
        echo "<td>".$arr[$key]."</td>";
    }
    echo "</tr>";
}

虽然,你会更好for...

于 2012-06-12T19:08:02.617 回答
1

如果使用 DIV 是一个选项:

<?php
    $array = array(
        array("aaa1", "aaa2", "aaa3", "aaa4"),
        array("bbb1", "bbb2", "bbb3", "bbb4"),
        array("ccc1", "ccc2", "ccc3", "ccc4"), 
        array("ddd1", "ddd2", "ddd3", "ddd4")
    );
    foreach($array as $abcd) {
        ?><div style="float: left;"><?php
        foreach($abcd as $oneTwoThree) {
            ?><div><?php
            echo $oneTwoThree;
            ?></div><?php
        }
        ?></div><?php
    }
?>
<div style="clear: both;"></div>
于 2012-06-12T19:16:03.430 回答