Bringing Query and Multidimensional Array

Asked

Viewed 80 times

1

I have the need to create an array more or less like 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 need to go through bringing the information in an organized way, aiming to create a table. The number of documents is variable and so is the number of fields, so you would need help with a malleable execution.

Thanks in advance for the help.

  • You’re abstracting a person’s data, right? You want to generate a table for each person or each person in a table row?

  • Each person is a row in the table. In fact I would like to do so: in CPF, for example, all the data relating to each field of the Cpf of all were grouped and so with all other fields.

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";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.