Skip to content Skip to sidebar Skip to footer

How To Merge Table Row With Php Array?

I want to merge the table row if the date have an same id's. I have some array data like this : Array ( [0] => Array ( [id] => 2 [date] =&

Solution 1:

I use the 'read ahead' technique for processing nested loops. It does mean that 'foreach' loops cannot be used as the next record must be read as soon as the current one has been processed. Basically, the last action you do in the loop is read the next record as you are setting it up for the next iteration. Note, you never test when to print a record as that is decided by the structure of the groups. The code loops are the same as the structure of the groups in the data

A 'group' is all the records with the same id.

I assume that the 'content' and 'act' are identical for each entry in the group.

Edited to add 'rowspan' attributes to the appropriate 'td' tags. I suspect css may be easier at this point.

The issue is that the group cannot be displayed until it is known how many entries are in it.

So, i 'buffer' all the records belonging to a group in an array. at the end of the group, it is displayed with the appropriate 'rowspan' attributes in the html.

It is tested on PHP 5.3.18. It includes test data.

<?php/* Q24028866 */$testData = array(array('id' => 2, 'date' => '05/13/2014', 'content' => 'some contents 2', 'act' => 'act1 act2 act3'),
                  array('id' => 2, 'date' => '05/28/2014', 'content' => 'some contents 2',  'act' => 'act1 act2 act3'),
                  array('id' => 7, 'date' => '06/04/2014', 'content' => 'some contents 7',  'act' => 'act1 act2 act3'),
                  array('id' => 8, 'date' => '06/08/2014', 'content' => 'some contents 8',  'act' => 'act1 act2 act3'),
                  array('id' => 8, 'date' => '06/09/2014', 'content' => 'some contents 8',  'act' => 'act1 act2 act3'));
?><!DOCTYPE html><html><head></head><body><tableborder='1'><thead><th>Date</th><th>Content</th><th>Act</th></thead><?php// use 'read ahead' so there is always a 'previous' record to compare against...$iterContents = new \ArrayIterator($testData);
$curEntry = $iterContents->current();

while ($iterContents->valid()) { // there are entries to process$curId = $curEntry['id'];

    // buffer the group to find out how many entries it has...$buffer = array();
    $buffer[] = $curEntry;

    $iterContents->next(); // next entry - may be same or different id...$curEntry = $iterContents->current();

    while ($iterContents->valid() && $curEntry['id'] == $curId) {  // process the group...$buffer[] = $curEntry; // store all records for a group in the buffer$iterContents->next(); // next entry - may be same or different id...$curEntry = $iterContents->current();
    }

     // display the current group in the buffer...echo'<tr>';
     echo'<td>', $buffer[0]['date'], '</td>';
     $rowspan = count($buffer) > 1 ? ' rowspan="'. count($buffer) .'"' : '';
     echo'<td', $rowspan, '>', $buffer[0]['content'], '</td>',
           '<td', $rowspan, '>', $buffer[0]['act'], '</td>';
     echo'</tr>';
     for($i = 1; $i < count($buffer); $i++) {
          echo'<tr><td>', $buffer[$i]['date'], '</td>';
          echo'</tr>';
     }
} ?></table></body></html>

Solution 2:

The following code will give you a clue what you should do,

$newArray = array();        
$counter=0;        
foreach($dataxas$key=>$val){        
    $newArray[$val['id']][$counter]['date'] = $val['date'];       
    $newArray[$val['id']][$counter]['content'] = $val['content'];        
    $newArray[$val['id']][$counter]['act'] = $val['act'];        
    $counter++;        
}        
$newArray = array_map('array_values', $newArray);

Post a Comment for "How To Merge Table Row With Php Array?"