Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:40:36+00:00 2026-06-15T23:40:36+00:00

The following two-dimensional array stores information for several images (1.Book that contains image, 2.Author

  • 0

The following two-dimensional array stores information for several images (1.Book that contains image, 2.Author of book that contains image, 3.Name of image, 4.Image 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));

A book can have several authors.
An author can be associated with several books.
An image can be contained in several books.

I would like to create an html table with the following columns:

  1. Book(s) – Field lists all the books that contain a specific image
  2. Author(s) – Field lists all the authors of all the books that contain that specific image
  3. Image Name – Field contains the name of that image

I am currently using the following code:

<?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);?>

My html code to create the table:

<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>

Resulting table:

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

Desired table:

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

Problem:
This code correctly outputs (2.) list of all authors of all the books that contain the image in question and (3.) the image name. However, it does not output (3.) as desired: It only outputs the first book_name (BookA) that contains the image in question, not a list of ALL books containing the image (BookA;BookB).

Question: How can I obtain an HTML table that contains 3 columns of which (1.) lists all books containing the image in question, (2.) lists all authors of the books containing the image in question, (3.) contains the name of the image in question.
Thanks a lot in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:40:36+00:00Added an answer on June 15, 2026 at 11:40 pm

    A few tweaks to your transform function, seems you handled the authors but forgot the books 🙂

    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);
    

    Output:

    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
            )
    
    )
    

    Which should hopefully work in your htmlout function

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following two-dimensional array stores information (Author, ID, and Title) for a collection of
I have the following as two ways to declare a two dimensional String array.
I have a two dimensional array with the following output Array ( [0] =>
I have a two-dimensional array. When I print/dump this I get the following My
I have the following three arrays and need to create a new two-dimensional array
I have the following two-dimensional array: 01 03 02 15 05 04 06 10
I have the following two dimensional array: static int[,] arr = new int[5, 5]
With my following code I try to sort a two dimensional array int[][] d2
I want to create an Object that contains one or more two dimensional arrays
I have the following two dimensional array in my java program. Integer[][] arrayOfSets =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.