title: Organize table in PHP
body: <p>I had already asked a similar question before, but I tried to adapt it to my code and it did not work well.</p>
  
  <p>I have the following code that paints a table in PHP, I want to show the <code>PRODUCT</code> total when I detect <code>PRODUCT</code> change, I have tried to do the following, but it shows me the <code>TOTAL</code> row at the beginning of each <code>PRODUCT</code> and does not totalize well</p>
  
  <p>The variables <code>GOLD</code>, <code>NORMAL</code> are dynamic, i.e. there may be tables that do not have <code>GOLD</code>, like others that do not have <code>NORMAL</code>:</p>
  
  <p>This is my code:</p>
  
  <pre><code>&lt;?php
  
  $harvest = Array ( 
         Array ( 
                       Array ( 
                                      'PRODUCT' =&gt; 'ROSE' ,
                                      'VARIETY' =&gt; 'ADELE', 
                                    'GOLD' =&gt; 160, 
                                      'NORMAL' =&gt; 0 ,
                                      'TOTAL' =&gt; 160 
                                    ) ,
                       Array ( 
                                      'PRODUCT' =&gt; 'ROSE',
                                      'VARIETY' =&gt; 'ALESSO' ,
                                      'GOLD' =&gt; 1320 ,
                                      'NORMAL' =&gt; 550, 
                                      'TOTAL' =&gt; 1870 
                                    ) ,
                       Array ( 
                                      'PRODUCT' =&gt; 'ROSE',
                                      'VARIETY' =&gt; 'ANASTACIA' ,
                                      'GOLD' =&gt; 440 ,
                                      'NORMAL' =&gt; 150 ,
                                      'TOTAL' =&gt; 590 
                                    ),
                       Array ( 
                                      'PRODUCT' =&gt; 'ROSE1',
                                    'VARIETY' =&gt; 'ANASTACIA1' ,
                                    'GOLD' =&gt; 420 ,
                                      'NORMAL' =&gt; 120 ,
                                      'TOTAL' =&gt; 540 
                                    ),
                       Array ( 
                                      'PRODUCT' =&gt; 'ROSE1',
                                      'VARIETY' =&gt; 'ANASTACIA1', 
                                      'GOLD' =&gt; 440 ,
                                      'NORMAL' =&gt; 100 ,
                                      'TOTAL' =&gt; 540 
                                    ),
                       Array ( 
                                      'PRODUCT' =&gt; 'ROSE2',
                                      'VARIETY' =&gt; 'ANASTACIA2',
                                      'GOLD' =&gt; 640, 
                                      'NORMAL' =&gt; 0, 
                                      'TOTAL' =&gt; 640 
                                    ),
                       Array ( 
                                      'PRODUCT' =&gt; 'ROSE2',
                                      'VARIETY' =&gt; 'ANASTACIA2' ,
                                      'GOLD' =&gt; 440, 
                                      'NORMAL' =&gt; 440, 
                                      'TOTAL' =&gt; 880 
                                     )
                      )
  
  
       );
  
  $arrayThead = array();
        for ($i=0; $i &lt; count($harvest) ; $i++) {
          array_push($arrayThead, array_keys($harvest[$i][0]));
        }
  
        $totalByProduct = array();
        foreach ($harvest as $items) {
            foreach ($items as $item) {
                if(!key_exists($item['PRODUCT'], $totalByProduct)){
                    $totalByProduct[$item['PRODUCT']] = $item;
                    continue;
                }
                foreach ($arrayThead as $key =&gt; $values) {
                  foreach ($values as $th) {
                    if($th != 'PRODUCT' &amp;&amp; $th != 'VARIETY'){
                      $totalByProduct[$item['PRODUCT']][$th] += $item[$th];
                    }
                  }
                }
            }
        }
        $arrayfoot= array();
  
        foreach ($harvest as $key =&gt; $value) {
          foreach ($value as $harv) {
            foreach ($arrayThead as $key =&gt; $values) {
              foreach ($values as $th) {
                if($th != 'PRODUCT' &amp;&amp; $th != 'VARIETY'){
                    $arrayfoot[$th] += $harv[$th];
                }
              }
            }
          }
        }
  
        $arrayComplete = array();
        for ($i=0; $i &lt; count($arrayThead) ; $i++) {
          for ($j=0; $j &lt; count($arrayThead[$i]) ; $j++) {
            if($arrayThead[$i][$j] != 'PRODUCT' &amp;&amp; $arrayThead[$i][$j] != 'VARIETY'){
              array_push($arrayComplete, $arrayThead[$i][$j]);
            }
          }
        }
  
  
        $arrayFinal = array();
        for ($j=0; $j &lt; count($arrayComplete) ; $j++) {
          array_push($arrayFinal, $arrayfoot[$arrayComplete[$j]]);
        }
  
        $body = '&lt;table style="border: 1px solid black;border-collapse: collapse;width: 100%;font-family:Calibri;"&gt;';
        $body .= '&lt;thead style="background-color:#f3f4f5;"&gt;';
        $body .= '&lt;tr&gt;';
  
        for ($i=0; $i &lt; count($arrayThead) ; $i++) {
          for ($j=0; $j &lt; count($arrayThead[$i]) ; $j++) {
            if($arrayThead[$i][$j] === 'PRODUCT' || $arrayThead[$i][$j] === 'VARIETY'){
              $body .= '&lt;th style="border: 1px solid black;height:50px;"&gt;'.$arrayThead[$i][$j].'&lt;/th&gt;';
            }else{
              $body .= '&lt;th style="border: 1px solid black;height:50px;"&gt;'.$arrayThead[$i][$j].'&lt;/th&gt;';
            }
          }
        }
        $body .= '&lt;/tr&gt;';
        $body .= '&lt;/thead&gt;';
        $body .= '&lt;tbody&gt;';
  
  //Initialize the variable product
        $product = "";
        foreach ($harvest as $items) {
            foreach ($items as $item) {
  
  //If the $product variable is different from the data product, print the total
  
            if($product != $item['PRODUCT']){
                $body .= '&lt;tr&gt;';
                $body .= '&lt;th style="border: 1px solid black;text-align:left;font-size:12px;"&gt;TOTAL '.$item['PRODUCT'].'&lt;/th&gt;';
                $body .= '&lt;th style="border: 1px solid black;text-align:left;font-size:12px;"&gt;&lt;/th&gt;';
  
                foreach ($arrayThead as $key =&gt; $values) {
                  foreach ($values as $th) {
                    if($th != 'PRODUCT' &amp;&amp; $th != 'VARIETY'){
                      $body .= '&lt;th style="border: 1px solid black;text-align:right;font-size:12px;"&gt;'.number_format($total).'&lt;/th&gt;';
                    }
                  }
                }
                $body .= '&lt;/tr&gt;';
                $product = $item['PRODUCT'];
            }
            $body .= '&lt;tr&gt;';
            foreach ($arrayThead as $key =&gt; $values) {
              foreach ($values as $th) {
                if($th === 'PRODUCT' || $th === 'VARIETY'){
                  $body .= '&lt;td style="border: 1px solid black;font-size:12px;"&gt;'.$item[$th].'&lt;/td&gt;';
                }else{
                  $body .= '&lt;td style="border: 1px solid black; text-align:right;font-size:12px;"&gt;'.number_format($item[$th]).'&lt;/td&gt;';
                }
              }
            }
            $body .= '&lt;/tr&gt;';
            $product = $item['PRODUCT'];
          }
        }
  
        $body .= '&lt;/tbody&gt;';
        $body .= '&lt;tfoot&gt;';
        $body .= '&lt;tr&gt;';
        $body .= '&lt;th style="border: 1px solid black;text-align:left;"&gt;TOTAL GENERAL&lt;/th&gt;';
        $body .= '&lt;th style="border: 1px solid black;"&gt;&lt;/th&gt;';
  
        for ($i=0; $i &lt; count($arrayFinal) ; $i++) {
          $body .= '&lt;th style="border: 1px solid black;text-align:right;"&gt;'.number_format($arrayFinal[$i]).'&lt;/th&gt;';
        }
  
        $body .= '&lt;/tr&gt;';
        $body .= '&lt;/tfoot&gt;';
        $body .= '&lt;/table&gt;';
  
        echo $body;
  </code></pre>
  
  <p>To see how it works, please copy and paste the code on the following page:</p>
  
  <p><a href="http://phptester.net/" rel="nofollow noreferrer">PHPTester</a></p>
  
  <p>I hope that someone can help me.</p>