【问题标题】:Table in PHP shows duplicate columns from arrayPHP中的表显示数组中的重复列
【发布时间】:2018-06-08 11:05:32
【问题描述】:

我编写了从数组变量写入表的代码,但它显示了重复的列:

这里是数组值:

    $data = [
    [
        "title" => "The World's End",
        "genre" => "Sci-fi",
        "year"  => 2013,
        "gross" => 21
    ],
    [
        "title" => "Scott Pilgrim vs. the World",
        "genre" => "Sadness",
        "year"  => 2010,
        "gross" => 21
    ],
    [
        "title" => "Hot Fuzz",
        "genre" => "Buddy Cop",
        "year"  => 2007,
        "gross" => 21
    ],
    [
        "title" => "Shaun of the Dead",
        "genre" => "Zombie",
        "year"  => 2007,
        "gross" => 21
    ],
];

和表写代码:

    <table>
    <tr>
        <?php
        foreach ($data as $item){
            foreach ($item as $key => $drop){
                echo '<th>' . $key . '</th>';
            }
        }
        ?>
    </tr>
    <?php foreach ($data as $item) : ?>
    <tr>
        <td><?= $item['title']; ?></td>
        <td><?= $item['genre']; ?></td>
        <td><?= $item['year']; ?></td>
        <td>€ <?= $item['gross']; ?></td>
        <?php endforeach; ?>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>€
            <?php
            $gross_sum = 0;
            foreach ($data as $gross_value)
            {
                $gross_sum += $gross_value['gross'];
            }
            echo $gross_sum;
            ?>
        </td>
    </tr>
</table>

如何从我的代码内容中删除表格中重复的右侧列?

【问题讨论】:

  • 为什么要以这种方式从数据中生成表头?我要么手动完成,要么拥有一个多维数组,其中一个键用于标题,一个用于数据

标签: php html-table multiple-columns


【解决方案1】:

这是因为您在开始时有一个嵌套的 foreach 循环,因此您将遍历 $data 中的每个条目,然后再次遍历 key。对于您的标题,您只需要遍历键一次。看看下面的代码,我对你的第一个 foreach 循环做了一些小改动。

<?php

$data = [
    [
        "title" => "The World's End",
        "genre" => "Sci-fi",
        "year"  => 2013,
        "gross" => 21
    ],
    [
        "title" => "Scott Pilgrim vs. the World",
        "genre" => "Sadness",
        "year"  => 2010,
        "gross" => 21
    ],
    [
        "title" => "Hot Fuzz",
        "genre" => "Buddy Cop",
        "year"  => 2007,
        "gross" => 21
    ],
    [
        "title" => "Shaun of the Dead",
        "genre" => "Zombie",
        "year"  => 2007,
        "gross" => 21
    ],
];
?>

<html>
    <table>
    <tr>
        <?php
        foreach ($data[0] as $key => $item){
            // foreach ($item as $key => $drop){
                echo '<th>' . $key . '</th>';
            // }
        }
        ?>
    </tr>
    <?php foreach ($data as $item) : ?>
    <tr>
        <td><?= $item['title']; ?></td>
        <td><?= $item['genre']; ?></td>
        <td><?= $item['year']; ?></td>
        <td>€ <?= $item['gross']; ?></td>
        <?php endforeach; ?>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>€
            <?php
            $gross_sum = 0;
            foreach ($data as $gross_value)
            {
                $gross_sum += $gross_value['gross'];
            }
            echo $gross_sum;
            ?>
        </td>
    </tr>
</table>
</html

【讨论】:

    【解决方案2】:

    尝试替换

    <tr>
        <?php
        foreach ($data as $item) {
            foreach ($item as $key => $drop) {
                echo '<th>' . $key . '</th>';
            }
        }
        ?>
    </tr>
    

    用这个:

    <tr>
        <?php
        foreach (array_keys($data[0]) as $item) {
            echo '<th>' . $item . '</th>';
        }
        ?>
    </tr>
    

    函数array_keys返回键显示在标题中。

    【讨论】:

    • 我在向下滚动之前将解决方案放在一起。这正是我所做的。为了使它更容易阅读,我确实将函数调用分配给$headers,但这只是个人偏好
    【解决方案3】:

    您可以使用current 获取数组的第一个元素,使用array_keys 获取键。将其结合在 foreach 中应该可以解决您的问题。

    foreach (array_keys(current($data)) as $key) {
        echo "<th>$key</th>";
    }
    

    除此之外,静态定义表头通常是更好的方法(除非,你不知道表格的内容),

    $header = ['title', 'genre', 'year', 'gross'];
    foreach ($header as $text) {
       echo "<th>$text</th>";
    }
    

    【讨论】:

      【解决方案4】:

      我以此为契机,按照我的意愿写了它。希望它对您有所帮助。

      <?php
      $data = [
          [
              "title" => "The World's End",
              "genre" => "Sci-fi",
              "year"  => 2013,
              "gross" => 21,
          ],
          [
              "title" => "Scott Pilgrim vs. the World",
              "genre" => "Sadness",
              "year"  => 2010,
              "gross" => 21,
          ],
          [
              "title" => "Hot Fuzz",
              "genre" => "Buddy Cop",
              "year"  => 2007,
              "gross" => 21,
          ],
          [
              "title" => "Shaun of the Dead",
              "genre" => "Zombie",
              "year"  => 2007,
              "gross" => 21,
          ],
      ];
      
      // Let's grab your headers
      $headers = array_keys($data[0]);
      
      // Format them by capitalizing the first letter
      $headers = array_map('ucfirst', $headers);
      
      /**
       * You could also inline it into one assignment
       * This decreases readability, but removes the quick reassignment of $header
       *
       * $headers = array_map('ucfirst', array_keys($data[0]));
       */
      
      /**
       * Let's calculate the gross here instead of in the presentation (HTML)
       */
      $total_gross = array_reduce($data, function ($gross, $movie) {
          $gross += $movie['gross'];
      
          return $gross;
      }, 0);
      ?>
      
      <table>
          <tr>
              <?php foreach ($headers as $header) :
                  printf('<th>%s</th>', $header);
              endforeach; ?>
          </tr>
          <?php foreach ($data as $movie) : ?>
              <tr>
                  <?php
                  printf('<td>%s</td>', $movie['title']);
                  printf('<td>%s</td>', $movie['genre']);
                  printf('<td>%s</td>', $movie['year']);
                  printf('<td>€ %s</td>', $movie['gross']);
                  ?>
              </tr>
          <?php endforeach; ?>
          <tr>
              <td colspan="3"></td>
              <?php printf('<td>€ %s</td>', $total_gross); ?>
          </tr>
      </table>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-02
        • 2012-07-16
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多