【发布时间】: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