Search code examples
phphtmlmpdf

merge 3 arrays and display the data inside horizontale HTML table


I have 3 arrays after merge these array I get this results

Here is the function I used for merge arrays

function mergeArrays(...$arrays)
 {
 
     $length = count($arrays[0]);
     $result = [];
     for ($i=0;$i<$length;$i++)
     {
         $temp = [];
         foreach ($arrays as $array)
             $temp[] = $array[$i];
 
         $result[] = $temp;
     }
 
     return $result;
 
 }

 $mg = mergeArrays($serviceName, $quantite, $prix);

Here is php inside mpdf

 <table style="width:100%;">
  <thead>
  <tr style="background-color: gray;">
  <th style="text-align:center;">SERVICE</th>
  <th style="text-align:center;">UNITE</th>
  <th style="text-align:center;">PRIX</th>
</tr>
<thead>
<tr>
';
for ($i = 0; $i < count($mg); $i++)
  for($j = 0; $j < count($mg); $j++):             
  $html .='
  <td>'.$mg[$i][$j].'</td>';
endfor;
$html.=
'
</tr>
</table>

Output enter image description here

I want to make a new line after each 3 column of data. Thank you for helping me


Solution

  • It looks like you're just missing the opening and closing <tr>s, in the outer for loop. You could add the tags like this:

    for ($i = 0; $i < count($mg); $i++) {
        $html .= '<tr>'; //open row
        for($j = 0; $j < count($mg); $j++) {
            $html .='<td>'.$mg[$i][$j].'</td>';
        }
        $html .= '</tr>'; //close row
    }