【问题标题】:How to display grouped data from two-dimensional array in HTML table?如何在 HTML 表格中显示二维数组中的分组数据?
【发布时间】:2012-11-29 10:31:42
【问题描述】:

以下二维数组存储书籍集合的信息(作者、ID 和标题):

$books = array(array('author'=>"AuthorA", 'ID'=>1, 'title'=>"titleA"),
               array('author'=>"AuthorB", 'ID'=>1, 'title'=>"titleA"),
               array('author'=>"AuthorC", 'ID'=>2, 'title'=>"titleB"),
               array('author'=>"AuthorC", 'ID'=>3, 'title'=>"titleC"),
               array('author'=>"AuthorD", 'ID'=>3, 'title'=>"titleC")); 

我目前使用 foreach 循环在 HTML 表格中显示此信息:

<table>
    <tr>
        <th>Author</th>
        <th>Book ID</th>
        <th>Title</th>
    </tr>
    <?php foreach ($books as $book): ?>               
    <tr>
        <td><?php htmlout($book['author']); ?></td>
        <td><?php htmlout($book['ID']); ?></td>
        <td><?php htmlout($book['title']); ?></td>
    </tr>
    <?php endforeach; ?>
</table>

此代码生成下表:

Author  | Book ID |  Title 
AuthorA      1       titleA
AuthorB      1       titleA
AuthorC      2       titleB
AuthorC      3       titleC
AuthorD      3       titleC

问题:我想以如下方式显示数据:

Authors            | Book ID |  Title 
AuthorA, Author B       1       titleA
AuthorC                 2       titleB
AuthorC, Author D       3       titleC

我怎样才能做到这一点?

【问题讨论】:

  • 这可能会有所帮助...whathaveyoutried.com
  • 您的数组没有您提到的字母数字键,例如author, ID
  • 对于多于一本的书籍,您如何将作者分组?
  • @Madbreaks:感谢您的评论。我更新了原帖。

标签: php html multidimensional-array foreach html-table


【解决方案1】:
$map = array();
foreach ($books as $book) {
    extract($book);
    $map[$ID][$title][] = $author;
}

foreach ($map as $ID => $entry) {
    foreach ($entry as $title => $authors) {
        printf(
            "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n"
          , join(',', $authors)
          , $ID
          , $title
        );
    }
}

【讨论】:

    【解决方案2】:

    你应该试试这段代码(我用 echo 而不是 htmlout):

    <?php
    $books = array(array("AuthorA", 1, "titleA"),
                   array("AuthorB", 1, "titleA"),
                   array("AuthorC", 2, "titleB"),
                   array("AuthorC", 3, "titleC"),
                   array("AuthorD", 3, "titleC")); 
    
    
    function transform($books) {
       $result = array();
       foreach($books as $key => $book) {
          $id = $book[1];
          if (!isset($result[$id])) {
             $result[$id] = array(
                'author' => array(),
                'ID' => $book[1],
                'title' => $book[2]
             );        
          }
          $result[$id]['author'] = array_merge( $result[$id]['author'], array($book[0]));
       }
       foreach($result as $key => $data) {
         $result[$key]['author'] = implode(', ', $result[$key]['author']);
       }
       return $result;
    }
    
    $books = transform($books);
    
    ?>
    
    <table>
        <tr>
            <th>Author</th>
            <th>Book ID</th>
            <th>Title</th>
        </tr>
        <?php foreach ($books as $book): ?>               
        <tr>
            <td><?php echo($book['author']); ?></td>
            <td><?php echo($book['ID']); ?></td>
            <td><?php echo($book['title']); ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
    

    【讨论】:

      【解决方案3】:
      <table border="1">
          <tr>
              <th>Author</th>
              <th>Book ID</th>
              <th>Title</th>
          </tr>
      <?php
      $books = array(array("AuthorA", 1, "titleA"),
                     array("AuthorB", 1, "titleA"),
                     array("AuthorC", 2, "titleB"),
                     array("AuthorC", 3, "titleC"),
                     array("AuthorD", 3, "titleC")); 
      $prev_key = 0;
      $authorColumn = "";
      $titleColumn = "";
      $rows = "";
      foreach ($books as $book){
          if($prev_key == 0){
              $authorColumn = $book['0'];
              $titleColumn = $book['2'];
              $prev_key = $book['1'];
          }elseif($prev_key == $book['1']){
              $authorColumn .= ",".$book['0'];
              $titleColumn .= ",".$book['2'];
          }else{
              $rows .= "<tr>
              <td>".$authorColumn."</td>
              <td>".$prev_key."</td>
              <td>".$titleColumn."</td>
          </tr>";
              $prev_key = $book['1'];
              $authorColumn = $book['0'];
              $titleColumn = $book['2'];
          }
      
      }
      $rows .= "<tr>
              <td>".$authorColumn."</td>
              <td>".$prev_key."</td>
              <td>".$titleColumn."</td>
          </tr>";
      echo $rows;
      
      ?>
      </table>
      

      此代码不干净,但运行正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-17
        • 2017-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-20
        • 2021-04-11
        • 2015-09-09
        相关资源
        最近更新 更多