Array for table problem with column

Asked

Viewed 31 times

0

all right? I’m having trouble converting this Array to an HTML table.

[1] => Array
    (
        [column] => Diagnóstico
        [date] => 30/08/2021
        [content] => TESTE 1
    )

[2] => Array
    (
        [column] => Dimensão
        [date] => 30/08/2021
        [content] => 01 - Políticas públicas
    )

[3] => Array
    (
        [column] => Tipo
        [date] => 30/08/2021
        [content] => Barreiras
    )

[4] => Array
    (
        [column] => Justificativa
        [date] => 30/08/2021
        [content] => Justificativa teste 1
    )

[5] => Array
    (
        [column] => Diagnóstico
        [date] => 30/08/2021
        [content] => TESTE 2
    )

[6] => Array
    (
        [column] => Dimensão
        [date] => 30/08/2021
        [content] => 01 - Políticas públicas
    )

[7] => Array
    (
        [column] => Tipo
        [date] => 30/08/2021
        [content] => Barreiras
    )

[8] => Array
    (
        [column] => Justificativa
        [date] => 30/08/2021
        [content] => Justificativa teste 2
    )

I would like to exhibit this were:

DATE Diagnosis Dimension Type Justification
30/08/2021 TEST 1 01 - Public policies Barriers Justification test 1
30/08/2021 TEST 2 01 - Public policies Barriers Justification test 2

I tried several forms of foreach and could not. I did it this way:

$columns = array();

foreach ($aTabela as $key2)
{
    $columns[$key2['column']] = $key2['column'];
}

echo '<table>';
echo '  <tr>';
echo '      <th>Data</th>';
foreach ($columns as $key2 => $value2)
{
    echo '      <th>' . $value2 . '</th>';
}
echo '      <th></th>';
echo '      <th></th>';
echo '  </tr>';

foreach ($aTabela as $key2 => $value2)
{
    echo '  <tr>';
    foreach ($columns as $key3 => $value3) {
        echo '<td>' . ((in_array($value3, $value2)) ? $value2[$value3] : 'n/a') . '</td>';
    }
    echo '  </tr>';
}
echo '</table>';

Does anyone have any suggestions? Thank you.

  • @Augustovasques thanks for the indication. The problem is that I have columns that repeat with different values. I am taking a beating for this code. If you have another suggestion would be very welcome.

  • When communicating an array to another programmer do not use the var_dump() use var_export() because the representation returned is a valid PHP code. So I would like to test your code but discouraged me from missing lunch hours by converting the $key array into something usable so I abandoned it.

No answers

Browser other questions tagged

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