Bringing Query and Array Multidimensiobnal

1

I have the need to create an array more or less as in the code below:

$documento = array();
$campo = array();

array_push($documento, "1", "CPF");
    array_push($campo, "1", "Nº CPF", "000.000.000-xx");
    array_push($campo, "2", "NOME TITULAR", "FULANO A");
array_push($documento, $campo);
array_push($documento, "2", "RG");
    array_push($campo, "1", "Nº RG", "x.xxx.xxx");
    array_push($campo, "2", "NOME TITULAR", "FULANO A");
array_push($documento, $campo);
array_push($documento, "3", "COMP ENDERECO");
    array_push($campo, "1", "Nº ENDERECO", "RUA DAS DUVIAS Nº 1");
    array_push($campo, "2", "NOME TITULAR","FULANO A");
array_push($documento, $campo);
array_push($documento, "1", "CPF");
    array_push($campo, "1", "Nº CPF", "111.111.111-xx");
    array_push($campo, "2", "NOME TITULAR", "FULANO B");
array_push($documento, $campo);
array_push($documento, "2", "RG");
    array_push($campo, "1", "Nº RG", "yy.yyyy.yyy");
    array_push($campo, "2", "NOME TITULAR", "FULANO B");
array_push($documento, $campo);
array_push($documento, "3", "COMP ENDERECO");
    array_push($campo, "1", "Nº ENDERECO", "RUA DAS DUVIAS Nº 2");
    array_push($campo, "2", "NOME TITULAR","FULANO B");
array_push($documento, $campo);

And then I have to go through the information in an organized way, in order to create a table. The number of documents is variable and the number of fields as well, so it would need help with a malleable execution.

Thanks for the help right away.

    
asked by anonymous 12.12.2016 / 20:17

1 answer

2

Multidimensional array with data shown in a table:

    # Dados das pessoas
    $pessoas = array(
        'fulanoA' => array(
            'nome' => 'fulanoA',
            'cpf' => '000.000.000-xx',
            'rg' => 'x.xxx.xxx',
            'endereco' => 'Qualquer endereco aqui'
        ),
        'fulanoB' => array(
            'nome' => 'fulanoB',
            'cpf' => '111.111.111.11',
            'rg' => '1115445',
            'endereco' => 'Qualquer endereco aqui',
            'outro' => 'Qualquer coisa aqui'
        )
    );
    # tabela
    echo "<table border='1'>
    <tbody>
    <tr><td>NOME</td>
    <td>CPF</td><td>RG</td>
    <td>ENDERECO</td>
    <td>OUTRO</td></tr>
    <tr>";
    foreach ($pessoas as $pessoa => $dados){
        foreach ($dados as $campo => $valor){
            echo "<td>".$valor."</td>\n";
        }
        echo "</tr>\n";
    }
    echo "</tbody>";
    echo "</table>\n";
    
13.12.2016 / 04:54