PHP - 将2d数组显示为表格

时间:2017-02-08 22:13:32

标签: php mysql arrays

我对PHP很新,我在以特定格式显示二维数组时遇到问题。

基本上,我知道如何正常打印2d阵列,但它看起来非常凌乱,我已经尝试过foreach但它并没有给我我想要的输出。我有一个2d数组,我想打印出来像一个5的表,所以如果数组中有35条记录,我应该打印7个表(每个表打印5个条目)。

我尝试使用0 - 7的forloop(对于表格)和forloop内部我使用foreach来访问数组元素,但是当我这样做时,它基本上从数组中打印了7次。

3 个答案:

答案 0 :(得分:0)

如果我了解您正在尝试做什么,这是一个解决方案:

foreach ($array as $subArray){
    foreach($subArray as $element){
        print "$element";
    }
    print "\n";
}

答案 1 :(得分:0)

    $cars = array
        (
        array("Volvo", 22, 18),
        array("BMW", 15, 13),
        array("Saab", 5, 2),
        array("Land Rover", 17, 15),
        array("benz", 252, 1558),
        array("tesla", 115, 193),
        array("chevy", 587, 211),
        array("ford", 13217, 115),
        array("Volvo", 22, 18),
        array("BMW", 15, 13),
        array("Saab", 5, 2),
        array("Land Rover", 17, 15),
        array("benz", 252, 1558),
        array("tesla", 115, 193),
        array("chevy", 587, 211),
        array("ford", 13217, 115),
        array("Volvo", 22, 18),
        array("BMW", 15, 13),
        array("Saab", 5, 2),
        array("Land Rover", 17, 15),
        array("benz", 252, 1558),
        array("tesla", 115, 193),
        array("chevy", 587, 211),
        array("ford", 13217, 115),
        array("Volvo", 22, 18),
        array("BMW", 15, 13),
        array("Saab", 5, 2),
        array("Land Rover", 17, 15),
        array("benz", 252, 1558),
        array("tesla", 115, 193),
        array("chevy", 587, 211),
        array("ford", 13217, 115),
        array("tesla", 115, 193),
        array("chevy", 587, 211),
        array("ford", 13217, 115),
    );

    $i = 0;
    foreach ($cars as $innerCar) {
        if ($i == 0) {
            echo "<table style='display:inline;'>";
            echo "<tbody>";
        }
        echo "<tr>";
        foreach ($innerCar as $car) {
            echo "<td>$car</td>";
        }
        echo "</tr>";
        if ($i == 5) {
            echo "</tbody>";
            echo "</table>";
            $i = 0;
        } else {
            $i++;
        }
    }

答案 2 :(得分:0)

另一种方法是使用数组索引和模数计算来确定将数组拆分到新表的位置:

$people = [
    [0115, 'Nguyen', 'Trang Luong Minh'],
    [0165, 'Rana', 'Shivam'],
    [0165, 'Family1', 'Given1'],
    [0165, 'Family2', 'Given2'],
    [0165, 'Family3', 'Given3'],
    [0165, 'Family4', 'Given4'],
    [0165, 'Family5', 'Given5'],
    [0165, 'Family6', 'Given6'],
    [0165, 'Family7', 'Given7'],
    [0165, 'Family8', 'Given8'],
    [0165, 'Family9', 'Given9'],
    [0165, 'Family10', 'Given10'],
  ];

$items_per_table = 5;

foreach ($people as $index => $person) {
  $item_index = $index % $items_per_table;

  if ($item_index == 0) {
    echo '<table>';
    echo '<tr><th>ID</th><th>Given Name</th><th>Family Name</th></tr>';  
  }

  echo '<tr>';
  foreach ($person as $data) {
    echo "<td>{$data}</td>";
  }
  echo '</tr>';

  if ($item_index == $items_per_table - 1 || count($people) == $index + 1) {
    echo '</table>';
  }
}