pass php matrix to html table

1

I am starting in PHP and I have been sent to create a table from a matrix, which is the following:

$array_notas=array(
            "Matemáticas"=>array("Primer trimestre"=>3,"Segundo trimestre"=>10,"Tercer trimestre"=>7),
            "Lengua"=>array("Primer trimestre"=>8,"Segundo trimestre"=>5,"Tercer trimestre"=>3),
            "Física"=>array("Primer trimestre"=>7,"Segundo trimestre"=>2,"Tercer trimestre"=>1),
            "Latín"=>array("Primer trimestre"=>4,"Segundo trimestre"=>7,"Tercer trimestre"=>8),
            "Ingles"=>array("Primer trimestre"=>6,"Segundo trimestre"=>2,"Tercer trimestre"=>3)
        );

I have tried several things, but they all go wrong, since I need to remove any cell from the array with the foreach loops, and that's it. I tried this but it does not show me the notes:

<html>
    <head>
        <title>Actividad 1</title>
    </head>
    <body bgcolor="#66CCFF"><center>
    <hr><h1>Boletín de notas</h1><hr>
    <?php
        $array_notas=array(
            "Matemáticas"=>array("Primer trimestre"=>3,"Segundo trimestre"=>10,"Tercer trimestre"=>7),
            "Lengua"=>array("Primer trimestre"=>8,"Segundo trimestre"=>5,"Tercer trimestre"=>3),
            "Física"=>array("Primer trimestre"=>7,"Segundo trimestre"=>2,"Tercer trimestre"=>1),
            "Latín"=>array("Primer trimestre"=>4,"Segundo trimestre"=>7,"Tercer trimestre"=>8),
            "Ingles"=>array("Primer trimestre"=>6,"Segundo trimestre"=>2,"Tercer trimestre"=>3)
        );
        ?>
        <table border=1>
    <tr>
        <td width=100 align="center"><b>Asignaturas</b></td>
        <td width=100 align="center"><b>Trimestre 1</b></td>
        <td width=100 align="center"><b>Trimestre 2</b></td>
        <td width=100 align="center"><b>Trimestre 3</b></td>
        <td width=100 align="center"><b>Media</b></td>
    </tr>
    <tr>
        <td align="center">'.$array_notas[0].'</td>
        <td align="right">'.$array_notas[0][0].'</td>
        <td align="right">'.$array_notas[0][1].'</td>
        <td align="right">'.$array_notas[0][2].'</td>
        <td align="right">6.7</td>
    </tr>
    <tr>
        <td align="center">'.$array_notas[1].'</td>
        <td align="right">'.$array_notas[1][0].'</td>
        <td align="right">'.$array_notas[1][1].'</td>
        <td align="right">'.$array_notas[1][2].'</td>
        <td align="right">5.3</td>
    </tr>
    <tr>
        <td align="center">'.$array_notas[2].'</td>
        <td align="right">'.$array_notas[2][0].'</td>
        <td align="right">'.$array_notas[2][1].'</td>
        <td align="right">'.$array_notas[2][2].'</td>
        <td align="right">3.3</td>
    </tr>
    <tr>
        <td align="center">'.$array_notas[3].'</td>
        <td align="right">'.$array_notas[3][0].'</td>
        <td align="right">'.$array_notas[3][1].'</td>
        <td align="right">'.$array_notas[3][2].'</td>
        <td align="right">6.3</td>
    </tr>
    <tr>
        <td align="center">'.$array_notas[4].'</td>
        <td align="right">'.$array_notas[4][0].'</td>
        <td align="right">'.$array_notas[4][1].'</td>
        <td align="right">'.$array_notas[4][2].'</td>
        <td align="right">3.7</td>
    </tr>
        </table>
        <p><h3><b>La media total es 5.1</b></h3>

    </body>
</html>
    
asked by Charly Utrilla 19.09.2018 в 11:56
source

1 answer

1

The first part of the table is correct. Define the titles of each column.

In the next part,

<tr>
    <td align="center">'.$array_notas[0].'</td>
    <td align="right">'.$array_notas[0][0].'</td>
    <td align="right">'.$array_notas[0][1].'</td>
    <td align="right">'.$array_notas[0][2].'</td>
    <td align="right">6.7</td>
</tr>

It's where the problems are.

1.- The first column is the title of the subject. In the array you define, it is the "key" of the first element. This is:

array_keys($array_notas)[0]; 

2.- each subject, in turn, has a key and its value. For example, for the second column, the key is Primer trimestre and its value is 3 . To access its value, you have to do:

$array_notas[0]['Primer trimestre'];

3.- for the following subjects, cambia la clave anterior .

4.- For the following elements, what you have to change is the primer indice . that is, the second row will be with index 1.

Finally, since you have an array defined with several elements, it is normal to go through the array row by row, and write the data with the corresponding variables. I'll leave this to you (it looks like it's a study thing, so check the loop part for it).

    
answered by 19.09.2018 / 12:12
source