1

以下二维数组存储多个图像的信息(1.包含图像的书,2.包含图像的书的作者,3.图像的名称,4.图像 ID):

$images = array(array('book_name'=>"BookA", 'author_name'=>"AuthorA",
                      'image_name'=>"ImageA", 'image_id'=>1),
                array('book_name'=>"BookA",'author_name'=>"AuthorB",
                      'image_name'=>"ImageA", 'image_id'=>1),
                array('book_name'=>"BookA",'author_name'=>"AuthorA",
                      'image_name'=>"ImageB", 'image_id'=>2),
                array('book_name'=>"BookA", 'author_name'=>"AuthorB",
                      'image_name'=>"ImageB", 'image_id'=>2),
                array('book_name'=>"BookB", 'author_name'=>"AuthorA",
                      'image_name'=>"ImageB", 'image_id'=>2));

一本书可以有多个作者。一个作者可以与几本书相关联。一个图像可以包含在几本书中。

我想创建一个包含以下列的 html 表:

  1. Book(s) - 字段列出包含特定图像的所有书籍
  2. 作者 - 字段列出了包含该特定图像的所有书籍的所有作者
  3. 图像名称 - 字段包含该图像的名称

我目前正在使用以下代码:

<?php function transform($images) {
    $result = array();
    foreach($images as $key => $image) {
        $image_id = $image['image_id'];
        if (!isset($result[$image_id])) {
            $result[$image_id] = array(
                                'author_name' => array(),
                                'book_name' => $image['book_name'],
                                'image_id' => $image['image_id'],
                                'image_name' => $image['image_name']
                                );        
        }       
    $result[$image_id]['author_name'] = array_merge($result[$image_id]['author_name'],     
    array(($image['author_name'])));
    }
    foreach($result as $key => $data) {
        $uniqueauthors = array_unique($result[$key]['author_name']);
    $result[$key]['author_name'] = implode('; ', $uniqueauthors);
    }
    return $result;
}
$images = transform($images);?>

我创建表格的 html 代码:

<table>
    <tr><th>Book(s)</th>
        <th>Author(s)</th>
        <th>Image Name</th>
    </tr>
    <?php foreach ($images as $image): ?>   
    <tr>
        <td><?php htmlout($image['book_name']); ?></td>
        <td><?php htmlout($image['author_name']); ?></td>
        <td><?php htmlout($image['image_name']); ?></td>
    </tr>
    <?php endforeach; ?>
</table>

结果表:

Book(s)       | Author(s)        | Image Name
BookA           AuthorA;AuthorB    ImageA
BookA           AuthorA;AuthorB    ImageB

所需表:

Book(s)       | Author(s)        | Image Name
BookA           AuthorA;AuthorB    ImageA
BookA;BookB     AuthorA;AuthorB    ImageB

问题: 此代码正确输出 (2.) 包含相关图像的所有书籍的所有作者的列表和 (3.) 图像名称。但是,它不会按需要输出 (3.):它只输出包含相关图像的第一个 book_name (BookA),而不是包含该图像 (BookA;BookB) 的所有书籍的列表。

问题:如何获得一个包含 3 列的 HTML 表,其中 (1.) 列出了包含相关图像的所有书籍,(2.) 列出了包含相关图像的书籍的所有作者,(3.) 包含相关图像的名称。提前非常感谢!

4

1 回答 1

1

对您的转换功能进行了一些调整,似乎您处理了作者但忘记了书籍:)

function transform($images)
{
    $result = array();
    foreach ($images as $key => $image)
    {
        $image_id = $image['image_id'];
        if (!isset($result[$image_id]))
        {
            $result[$image_id] = array(
                'author_name' => array(),
                'book_name' => array(),
                'image_id' => $image['image_id'],
                'image_name' => $image['image_name']
            );
        }
        $result[$image_id]['book_name'] = array_merge($result[$image_id]['book_name'], array($image['book_name']));
        $result[$image_id]['author_name'] = array_merge($result[$image_id]['author_name'], array($image['author_name']));
    }
    foreach ($result as $key => $data)
    {
        $uniquebooks = array_unique($result[$key]['book_name']);
        $result[$key]['book_name'] = implode('; ', $uniquebooks);
        $uniqueauthors = array_unique($result[$key]['author_name']);
        $result[$key]['author_name'] = implode('; ', $uniqueauthors);
    }
    return $result;
}

$images = transform($images);

print_r($images);

输出:

Array
(
    [1] => Array
        (
            [author_name] => AuthorA; AuthorB
            [book_name] => BookA
            [image_id] => 1
            [image_name] => ImageA
        )

    [2] => Array
        (
            [author_name] => AuthorA; AuthorB
            [book_name] => BookA; BookB
            [image_id] => 2
            [image_name] => ImageB
        )

)

这应该有望在您的 htmlout 函数中起作用

于 2012-12-17T13:46:53.823 回答