【问题标题】:Retrieving and displaying data stored in multi-dimensional array检索和显示存储在多维数组中的数据
【发布时间】:2012-12-04 14:29:06
【问题描述】:

以下二维数组存储多个图像的信息(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. 图书 - 字段列出包含特定图片的所有图书
  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.) 包含有问题的图像的名称。 提前非常感谢!

【问题讨论】:

    标签: php function multidimensional-array foreach array-merge


    【解决方案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 函数中起作用

    【讨论】:

    • 是的,我忘记了 foreach 循环中的书籍。感谢 Dale,您的解决方案有效!
    猜你喜欢
    • 2012-03-07
    • 2010-10-27
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 2019-11-18
    • 2014-09-09
    • 2017-06-01
    相关资源
    最近更新 更多