1

次の 2 次元配列は、いくつかの画像の情報を格納します (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));

1 つの本に複数の著者がいる場合があります。著者は複数の書籍に関連付けることができます。1 つのイメージを複数のブックに含めることができます。

次の列を持つ html テーブルを作成したいと思います。

  1. 本 - フィールドには、特定の画像を含むすべての本が一覧表示されます
  2. Author(s) - フィールドには、その特定の画像を含むすべての書籍のすべての著者が一覧表示されます
  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) を出力しません。

質問: (1.) 問題の画像を含むすべての書籍の一覧、(2.) 問題の画像を含む書籍のすべての著者の一覧、(3.) を含む 3 つの列を含む HTML テーブルを取得するにはどうすればよいですか?問題の画像の名前。よろしくお願いします!

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