0

The following two-dimensional array stores information (Author, ID, and Title) for a collection of books:

$books = array(array('author'=>"AuthorA", 'ID'=>1, 'title'=>"titleA"),
               array('author'=>"AuthorB", 'ID'=>1, 'title'=>"titleA"),
               array('author'=>"AuthorC", 'ID'=>2, 'title'=>"titleB"),
               array('author'=>"AuthorC", 'ID'=>3, 'title'=>"titleC"),
               array('author'=>"AuthorD", 'ID'=>3, 'title'=>"titleC")); 

I currently use a foreach loop to present this information in an HTML table:

<table>
    <tr>
        <th>Author</th>
        <th>Book ID</th>
        <th>Title</th>
    </tr>
    <?php foreach ($books as $book): ?>               
    <tr>
        <td><?php htmlout($book['author']); ?></td>
        <td><?php htmlout($book['ID']); ?></td>
        <td><?php htmlout($book['title']); ?></td>
    </tr>
    <?php endforeach; ?>
</table>

This code generates the following table:

Author  | Book ID |  Title 
AuthorA      1       titleA
AuthorB      1       titleA
AuthorC      2       titleB
AuthorC      3       titleC
AuthorD      3       titleC

Question: I would like to display the data in the following way:

Authors            | Book ID |  Title 
AuthorA, Author B       1       titleA
AuthorC                 2       titleB
AuthorC, Author D       3       titleC

How can I achieve this?

3
  • This might help... whathaveyoutried.com Commented Dec 11, 2012 at 20:02
  • 1
    Your array doesn't have alphanumeric keys like you mention, e.g. author, ID Commented Dec 11, 2012 at 20:04
  • How are you grouping the authors together for books which have more than one? Commented Dec 11, 2012 at 20:15

3 Answers 3

1
$map = array();
foreach ($books as $book) {
    extract($book);
    $map[$ID][$title][] = $author;
}

foreach ($map as $ID => $entry) {
    foreach ($entry as $title => $authors) {
        printf(
            "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n"
          , join(',', $authors)
          , $ID
          , $title
        );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You should try this code (I used echo instead of htmlout):

<?php
$books = array(array("AuthorA", 1, "titleA"),
               array("AuthorB", 1, "titleA"),
               array("AuthorC", 2, "titleB"),
               array("AuthorC", 3, "titleC"),
               array("AuthorD", 3, "titleC")); 


function transform($books) {
   $result = array();
   foreach($books as $key => $book) {
      $id = $book[1];
      if (!isset($result[$id])) {
         $result[$id] = array(
            'author' => array(),
            'ID' => $book[1],
            'title' => $book[2]
         );        
      }
      $result[$id]['author'] = array_merge( $result[$id]['author'], array($book[0]));
   }
   foreach($result as $key => $data) {
     $result[$key]['author'] = implode(', ', $result[$key]['author']);
   }
   return $result;
}

$books = transform($books);

?>

<table>
    <tr>
        <th>Author</th>
        <th>Book ID</th>
        <th>Title</th>
    </tr>
    <?php foreach ($books as $book): ?>               
    <tr>
        <td><?php echo($book['author']); ?></td>
        <td><?php echo($book['ID']); ?></td>
        <td><?php echo($book['title']); ?></td>
    </tr>
    <?php endforeach; ?>
</table>

Comments

1
<table border="1">
    <tr>
        <th>Author</th>
        <th>Book ID</th>
        <th>Title</th>
    </tr>
<?php
$books = array(array("AuthorA", 1, "titleA"),
               array("AuthorB", 1, "titleA"),
               array("AuthorC", 2, "titleB"),
               array("AuthorC", 3, "titleC"),
               array("AuthorD", 3, "titleC")); 
$prev_key = 0;
$authorColumn = "";
$titleColumn = "";
$rows = "";
foreach ($books as $book){
    if($prev_key == 0){
        $authorColumn = $book['0'];
        $titleColumn = $book['2'];
        $prev_key = $book['1'];
    }elseif($prev_key == $book['1']){
        $authorColumn .= ",".$book['0'];
        $titleColumn .= ",".$book['2'];
    }else{
        $rows .= "<tr>
        <td>".$authorColumn."</td>
        <td>".$prev_key."</td>
        <td>".$titleColumn."</td>
    </tr>";
        $prev_key = $book['1'];
        $authorColumn = $book['0'];
        $titleColumn = $book['2'];
    }

}
$rows .= "<tr>
        <td>".$authorColumn."</td>
        <td>".$prev_key."</td>
        <td>".$titleColumn."</td>
    </tr>";
echo $rows;

?>
</table>

This code is not clean but it's working correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.