0

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

我怎样才能做到这一点?

4

3 回答 3

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>
于 2012-12-11T20:36:52.553 回答
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>

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

于 2012-12-11T20:46:00.297 回答
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
        );
    }
}
于 2012-12-11T21:11:07.067 回答