将一个表、两列PHP关联数组打印为三个并排的两列表


Print one table, two-column PHP associative array as three side-by-side, two-column tables

我使用以下代码在PHP中从关联数组中打印一个15行两列的表:

<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
    <?
    $rowCount = 0;
    foreach($word_frequency as $word => $frequency) if ($rowCount++ < 15) {?>
        <tr>
            <td><? echo $word; ?></td>
            <td<? echo $frequency; ?></td>
        </tr>
    <?}?>
</tbody>
</table>

它工作得很好,但是在页面上占用了太多的垂直空间。我如何将其格式化为三个并排的五行两列表,每个表的第一组有数组项1-5,第二组6-10,最后一组11-15?(参考下图):

key1  value1  |  key6   value6   |  key11   value11
key2  value2  |  key7   value7   |  key12   value12
...
...
key5  value5  |  key10  value10  |  key15   value15 

我尝试过各种表,div,容器和多个循环实验,结果非常混合(和不合适)。

由于您也可以使用多列来实现这种视觉效果,因此您可能希望提前格式化数据,以使生成表的代码更简洁。

<?php
  // Format the array data so each row contains all of the columns needed
  $rows = array();
  $max_per_column = 5;
  $max_words = 15;
  $rows = array_pad($rows, $max_per_column, array());
  $count = 0;
  foreach ($word_frequency as $word => $frequency) {
    if ($count >= $max_words) {
      break;
    }
    array_push($rows[$count % $max_per_column], $word, $frequency);
    $count++;
  }
?>
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
  <tbody>
    <?php
      foreach ($rows as $cols) {
        echo '<tr><td>' . implode('</td><td>', $cols) . '</td></tr>';
      }
    ?>
  </tbody>
</table>

试试这样做。

<table width="100%">
<tr>
<?php
    $rowCount = 0;
    foreach($word_frequency as $word => $frequency) if ($rowCount < 15) { ?>
   <?php if($rowCount % 5 == 0) {  ?>
     <td><table border=1>
   <?php } ?>                   
       <tr>
         <td><?php echo $word; ?></td>
         <td><?php echo $frequency; ?></td>
       </tr>
       <?php if($rowCount % 5 == 4) {  ?>
     </table></td>
   <?php } ?>                   
<?php $rowCount++; } ?>
</tr>
</table>

唯一的方法是使用数字键。例如

 $word_frequency[0] = array();
 $word_frequency[0]['word'] = "some word";
 $word_frequency[0]['frequency'] = 10;
 ...
 $word_frequency[15] = array();
 $word_frequency[15]['word'] = "some other word";
 $word_frequency[15]['frequency'] = 14;
一旦你有了你的数组,你可以重复这个循环,如下所示
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
  <tbody>
<?
$rowCount = 0;
 for($i = 0; $i <= 5; $i++) { 
    $word[0] = $word_frequency[$i]["word"];
    $frequency[0] = $word_frequency[$i]["frequency"];
    $word[1] = $word_frequency[$i + 5]["word"];
    $frequency[1] = $word_frequency[$i + 5]["frequency"];
    $word[2] = $word_frequency[$i + 10]["word"];
    $frequency[2] = $word_frequency[$i + 10]["frequency"];
    ?>
     <tr>
    <?
    for($x = 0; $x <= 2; $x++ ){
 ?>
        <td><? echo $word[$x]; ?></td>
        <td<? echo $frequency[$x]; ?></td>
    <?
    }
    ?>
     </tr>
    <?
  }
    ?>
 </tbody>
 </table>

每个$i循环创建行,而$x循环创建列。当然,这假定您至少有15个项目,并且您将只创建5行和3列的键/值对。

如果我要做的话,我可能会使用一个带有css columns的ul:D

http://davidwalsh.name/css-columns

然而,这并不是一个非常"程序化"的事情。

不必按顺序遍历数组可能会有所帮助。

如果你看看你有什么,有一个模式的索引:

i + 0x, i + 1x, i + 2x,…

其中x是条目数除以列数。

,当I = total/x

时停止迭代

对不起,我太累了,不能为你写这个,但要点是:

$myarray = [1 ... 15];
$columns = 3;
$arrayKeys = array_keys($myarray);
$total = count($array);
$x = floor $total  / $column;
for ($i = 0; $i > $x; $i += $x + 1 ){
  echo $arrayKeys[$i] . " - " . $myarray($arrayKeys[$i]);
  echo $arrayKeys[$i + $x] . " - " . $myarray($arrayKeys[$i + $x]);
  echo $arrayKeys[$i + ($x * 2) ] . " - " . $myarray($arrayKeys[$i + ($x * 2)]);
}

或者类似的东西…这是我暂时想到的,我认为它有一些问题,但你应该能够把它变成可行的东西。