Problems generating a table with php from a two-dimensional array

1

Hello this is my code and although I put the rowspan to 3 does not generate the table correctly, I try to generate a table from a two-dimensional array with PHP but when formatting the table does not come out as it should, I can not find the error, please help.

<table border="1">  
    <?php
    //Primero creamos dos arrays $matriz_pablo y $matriz_roberto para luego insertarlas en un array general $matriz_general la que sera bidimensional
    $matriz_pablo = array('nombre' => 'pablo', 'profesion' => 'ministro', 'edad' => '50');
    $matriz_roberto = array('nombre' => 'roberto', 'profesion' => 'agricultor', 'edad' => '45');
    $matriz_general = array('PABLO' => $matriz_pablo,'ROBERTO' => $matriz_roberto);
    //A continuación imprimimos los valores de la matriz bidimensional.
    //El Primer FOREACH muestra las claves de primer nivel PABLO y ROBERTO
    foreach ($matriz_general as $key => $value) {
        echo "<tr>"."<td rowspan='3'>".$key."</td>";//Se muetra la Clave
            //El Segundo FOREACH mostrará las claves y valores de segundo nivel internos de la columna valor de primer nivel.
            foreach ($value as $clave => $valor) {
                echo "<tr>"."<td>".$clave."</td>"."<td>".$valor."</td>"."</tr>";
            }
        echo "</tr>";
    }
    ?>
</table>
    
asked by Jean Devonne 09.01.2018 в 23:55
source

2 answers

1

I was analyzing the code and I think I managed to solve it with rowspan="3" by removing one of the tags and lowering a level the td

<table border="1">  
    <?php
    //Primero creamos dos arrays $matriz_pablo y $matriz_roberto para luego insertarlas en un array general $matriz_general la que sera bidimensional
    $matriz_pablo = array('nombre' => 'pablo', 'profesion' => 'ministro', 'edad' => '50');
    $matriz_roberto = array('nombre' => 'roberto', 'profesion' => 'agricultor', 'edad' => '45');
    $matriz_general = array('PABLO' => $matriz_pablo,'ROBERTO' => $matriz_roberto);
    //A continuación imprimimos los valores de la matriz bidimensional.
    //El Primer FOREACH muestra las claves de primer nivel PABLO y ROBERTO
    foreach ($matriz_general as $key => $value) {
        echo "<tr>"."<td rowspan='3'>".$key."</td>";//Se muetra la Clave
            //El Segundo FOREACH mostrará las claves y valores de segundo nivel internos de la columna valor de primer nivel.
            foreach ($value as $clave => $valor) {
                echo "<td>".$clave."</td>"."<td>".$valor."</td>"."</tr>";
            }
    }
    ?>
</table>
    
answered by 10.01.2018 / 16:46
source
1

If you wait for a table like this as a result:

    <table border="1">

		<! fila 1->
        <tr>
            <td rowspan='4'>PABLO</td>
        </tr>

		<! fila 2->
        <tr>
            <td>nombre</td>

            <td>pablo</td>
        </tr>

		<! fila 3->
        <tr>
            <td>profesion</td>

            <td>ministro</td>
        </tr>

		<! fila 4->
        <tr>
            <td>edad</td>

            <td>50</td>
        </tr>

        <tr>
            <td rowspan='4'>ROBERTO</td>
        </tr>

        <tr>
            <td>nombre</td>

            <td>roberto</td>
        </tr>

        <tr>
            <td>profesion</td>

            <td>agricultor</td>
        </tr>

        <tr>
            <td>edad</td>

            <td>45</td>
        </tr>
    </table>

You have to change echo "<tr>"."<td rowspan='3'>".$key."</td>"; for this: echo "<tr>"."<td rowspan='4'>".$key."</td>"; , because there are 4 rows that you want to join, not 3.

Or, raise another way to create the table.

    
answered by 10.01.2018 в 00:19