如何在HTML表格中显示二维数组的分组数据?

时间:2012-12-11 20:01:27

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

以下二维数组存储书籍集合的信息(作者,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

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:1)

你应该尝试这个代码(我用的是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>

答案 1 :(得分:1)

<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>

此代码不干净但工作正常。

答案 2 :(得分: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
        );
    }
}