在PHP

时间:2015-10-23 21:01:12

标签: php arrays multidimensional-array

这里我有一个存储有关某些状态的信息的多维数组。所有的信息都显示出来,但我把它格式化成4x11的表格很麻烦。

<?php

$stateinfo = array
(
        array("Alabama","Montgomery","4,779,736",23),
        array("Alaska", "Juneau", "710,231", 47),
        array("Arizona","Phoenix","6,329,017", 18),
        array("Arkansas","Little Rock", "2,915,918", 32),
        array("California","Sacramento","37,253,956",1),
        array("Colorado","Denver","5,029,196",22),
        array("Connecticut","Hartford","3,518,288",29),
        array("Delaware","Dover","897,934",45),
        array("Florida","Tallahassee","18,801,310",4),
        array("Georgia","Atlanta","9,687,653",9),
        array("Hawaii","Honolulu","1,360,301",42)
);

 for($row = 0; $row <count($stateinfo);$row++)
 {
        for($col=0;$col <count($stateinfo[$row]);$col++)
        {
                echo $stateinfo[$row][$col];
        }
         echo "<br>";
 }
 ?>

当前输出:

enter image description here

需要输出:

enter image description here

2 个答案:

答案 0 :(得分:-1)

我会这样做:

<table>
<?php
foreach($stateinfo as $row) {
    echo '<tr><td>'.implode('</td><td>', $row).'</td></tr>';
}
?>
</table>

然后将样式应用于表/ td以获得特定的间距。

答案 1 :(得分:-2)

好像你只想要一些格式化?然后,当您循环打印多维数组时,只需在每个元素或&nbsp;之间添加一些padding:5px;即可。实际上有一百万种方法可以做到这一点:

  • 通过默认HTML表格显示
  • 使用无休息空格
  • 用填充或边距包裹`span`
  • ...