The $freqs array contains the counts for P1, P2 , P3 for each digit...
$freqs = Array
(
[0] => Array # digit "0"
(
[0] => 4 # P1
[1] => 7 # P2
[2] => 1 # P3
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 6
)
[2] => Array
(
[0] => 4
[1] => 4
[2] => 6
)
which, coincidentally, is the same structure as the output table. You now loop through the array and for each digit (row) loop through its array (positions columns) and build the table.
//
// create frequncy table and calc digit totals
//
$totals = array_fill_keys(range(0,9), []);
$tdata = '';
foreach ($freqs as $n => $occs) {
$tdata .= "<tr><td><b>$n</b></td>";
foreach ($occs as $o) {
$tdata .= "<td>$o</td>";
}
$total = array_sum($occs);
$totals[$n] = [$n,$total];
$tdata .= "<td>=</td><td><b>$total</b></td></tr>\n";
}
My complete solution...