0

次の2次元配列には、書籍のコレクションの情報(著者、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

このコードを試してみてください(私はhtmloutの代わりにechoを使用しました):

<?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 に答える