 <p>Your first <code>foreach()</code> is causing the issue, as it is looping through both subarrays before printing the second foreach loop. Try removing it, and just reference the first <code>$v['kaupunki_id']</code> using <code>$values[0]['kaupunki_id']</code> -</p> <pre><code>$numerointi =1; echo "&lt;table class=\"zebra\"&gt;"; foreach($arrayTotals as $team=&gt;$values){ echo "&lt;tr class=\"" . $values[0]['kaupunki_id'] . "\"&gt;&lt;td&gt;".$numerointi ."."; echo "&lt;td&gt;" . $team; $sum1=0; $sum2=0; $numerointi ++; foreach($values as $v){ echo "&lt;td class=\"pisteet\"&gt;" . $v['pisteet_1'] . "/" . $v['pisteet_2'] . "&lt;/td&gt;"; $sum1 +=$v['pisteet_1']; $sum2 +=$v['pisteet_2']; } echo '&lt;td class="summa"&gt;'.$sum1.'/'.$sum2."&lt;/td&gt;"; echo "&lt;/tr&gt;"; } echo '&lt;/table&gt;'; </code></pre>