0

Assume I have this array from a query results:

[0]=> { ["category"]=> "fruit" ["value"]=> "banana"} 
[1]=> { ["category"]=> "fruit" ["value"]=> "grape"}
[2]=> { ["category"]=> "fruit" ["value"]=> "pineapple"}    
[3]=> { ["category"]=> "animal" ["value"]=> "mouse"}    
[4]=> { ["category"]=> "animal" ["value"]=> "elephant"}    
[5]=> { ["category"]=> "animal" ["value"]=> "ant"}   

How to make an HTML table which same group shows only once and make td rowspanvalue in group field(category) according to number of members?

Desired result:

--------------------
|fruit   |banana   |
|        -----------
|        |grape    |
|        -----------
|        |pinapple |
--------------------
|animal  |mouse    |
|        -----------
|        |elephant |
|        -----------
|        |ant      |
---------------------

1 Answer 1

2

Create a different array.

$truecats = array();
foreach ($cats as $pieces) {
    if (!isset($truecats[$pieces['category']]) {
        $truecats[$pieces['category']] = array();
    }
    $truecats[$pieces['category']][] = $pieces['value'];
}

// You can then loop over this array:

foreach ($truecats as $name => $values) {
   echo "<tr><td rowspan=" . count($values) . ">$name</td>";
   foreach ($values as $val) {
      echo "<td>$val</td>";
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

For second and next member we need adding new row (tr)

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.