<html>
    <head>
        <style>
            select {width:160px;}
            td, body {font-family:Verdana; font-size:10pt}
        </style>
    </head>
    <body>
        <table border width=500>
            <tr>
                <td>Nome</td><td>Cognome</td><td>Matricola</td>
                <?
                    //lettura file
                    $f1 = file("File1.txt");
                    $f2 = file("File2.txt");
                    //costruisco array associativo Materia -> Array(n°studenti) -> Array(DatiStudente)
                    //Questo metodo permette di avere un array con le materie NON ripetute
                    foreach ($f2 as $row2) {
                        $field2 = explode(";", $row2);
                        $materie[$field2[0]][] = $field2;
                    }
                    //scrive materie nella tabella
                    foreach ($materie as $p)
                        echo "<td>" . $p[0][0] . "</td>";
                ?>
                <td>Media Pesata</td>
            </tr>
            <?
                foreach ($f1 as $row1) {
                    $field1 = explode(";", $row1);
                    //scrivo in tabella Nome, Cognome, Matricola
                    echo "<tr>"; for ($i=0;$i<3;$i++) echo "<td>$field1[$i]</td>";
                    $t = $num = 0;
                    //Analizzo le materie
                    foreach ($materie as $p) {                    
                        $sw = false;
                        //Analizzo studenti registrati x ogni materia
                        foreach ($p as $pntall) {
                            if ((integer)$field1[2]==(integer)$pntall[1]) {
                                echo "<td>" . $pntall[2] . "</td>";
                                //media pesata
                                $t += (integer)$pntall[2] * $pntall[3];
                                $num += $pntall[3];
                                $sw = true;
                            }
                        }
                        if (!$sw) echo "<td> - </td>";
                    }
                    echo "<td>" . round($t/$num,0) . "</td></tr>";
                }
                echo "</table>";
                
                
                //mostra come l'array č organizzato (solo x chiarezza, NON c'entra con l'esercizio
                echo "<hr><h4>Struttura dell'array utilizzato (Array associativo)</h4>";
                // visualizzazione della struttura dell'array m$materie
                foreach ($materie as $p) {
                    $nallreg = count($p);
                    echo "<b>Materie[" . $p[0][0] .
                         "]</b><br>C'č/ci sono $nallreg studente/i registrato/i (sono sottoarray)<br><ul>";
                    $cnt = 0;
                    foreach ($p as $numall) {
                        echo "<li>Materie[" . $p[0][0] . "][" . $cnt . "]><ul>";
                            $cnt2 = 0;
                            foreach($numall as $datiall) {
                                echo "<li>Materie[" . $p[0][0] . "][" . $cnt . "][" . $cnt2++ . "] : " . $datiall;
                            }
                        echo "</ul><br>";
                        $cnt++;
                    }
                    echo "</ul>";
                }
            ?>
        </table>
    </body>
</html>