title: Retrieving and displaying data stored in multi-dimensional array
body: <p>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):</p>
  
  <pre><code>$images = array(array('book_name'=&gt;"BookA", 'author_name'=&gt;"AuthorA",
                        'image_name'=&gt;"ImageA", 'image_id'=&gt;1),
                  array('book_name'=&gt;"BookA",'author_name'=&gt;"AuthorB",
                        'image_name'=&gt;"ImageA", 'image_id'=&gt;1),
                  array('book_name'=&gt;"BookA",'author_name'=&gt;"AuthorA",
                        'image_name'=&gt;"ImageB", 'image_id'=&gt;2),
                  array('book_name'=&gt;"BookA", 'author_name'=&gt;"AuthorB",
                        'image_name'=&gt;"ImageB", 'image_id'=&gt;2),
                  array('book_name'=&gt;"BookB", 'author_name'=&gt;"AuthorA",
                        'image_name'=&gt;"ImageB", 'image_id'=&gt;2));
  </code></pre>
  
  <p>A book can have several authors.
  An author can be associated with several books. 
  An image can be contained in several books.</p>
  
  <p>I would like to create an html table with the following columns:</p>
  
  <ol>
  <li>Book(s) - Field lists all the books that contain a specific image</li>
  <li>Author(s) - Field lists all the authors of all the books that contain that specific image </li>
  <li>Image Name - Field contains the name of that image </li>
  </ol>
  
  <p>I am currently using the following code:</p>
  
  <pre><code>&lt;?php function transform($images) {
      $result = array();
      foreach($images as $key =&gt; $image) {
          $image_id = $image['image_id'];
          if (!isset($result[$image_id])) {
              $result[$image_id] = array(
                                  'author_name' =&gt; array(),
                                  'book_name' =&gt; $image['book_name'],
                                  'image_id' =&gt; $image['image_id'],
                                  'image_name' =&gt; $image['image_name']
                                  );        
          }       
      $result[$image_id]['author_name'] = array_merge($result[$image_id]['author_name'],     
      array(($image['author_name'])));
      }
      foreach($result as $key =&gt; $data) {
          $uniqueauthors = array_unique($result[$key]['author_name']);
      $result[$key]['author_name'] = implode('; ', $uniqueauthors);
      }
      return $result;
  }
  $images = transform($images);?&gt;
  </code></pre>
  
  <p>My html code to create the table:</p>
  
  <pre><code>&lt;table&gt;
      &lt;tr&gt;&lt;th&gt;Book(s)&lt;/th&gt;
          &lt;th&gt;Author(s)&lt;/th&gt;
          &lt;th&gt;Image Name&lt;/th&gt;
      &lt;/tr&gt;
      &lt;?php foreach ($images as $image): ?&gt;   
      &lt;tr&gt;
          &lt;td&gt;&lt;?php htmlout($image['book_name']); ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php htmlout($image['author_name']); ?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php htmlout($image['image_name']); ?&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;?php endforeach; ?&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Resulting table:</p>
  
  <pre><code>Book(s)       | Author(s)        | Image Name
  BookA           AuthorA;AuthorB    ImageA
  BookA           AuthorA;AuthorB    ImageB
  </code></pre>
  
  <p>Desired table:</p>
  
  <pre><code>Book(s)       | Author(s)        | Image Name
  BookA           AuthorA;AuthorB    ImageA
  BookA;BookB     AuthorA;AuthorB    ImageB
  </code></pre>
  
  <p><strong>Problem:</strong> 
  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). </p>
  
  <p><strong>Question:</strong> 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! </p>