1
Hello. In the system I am developing I have the possibility to create filters for queries, but queries do not always have as a result the same columns. The result is stored in a key-value array, where the key is the name of each BD column.
Example of query result:
Array
(
[0] => Array
(
[indicador] => Precipitação total
[fonte_de_dados] => INMET
[microrregiao] => Campanha Meridional
[sigla_uf] => RS
[mes_coleta] => 1/2020
[valor] => 0.140
[unidade] => mm
)
[1] => Array
(
[indicador] => Precipitação total
[fonte_de_dados] => INMET
[microrregiao] => Caxias do Sul
[sigla_uf] => RS
[mes_coleta] => 1/2020
[valor] => 0.194
[unidade] => mm
)
)
With this in mind, to avoid several "if...else
", I intend to display at first in a table the name of each key (indicator, data fonte_de_data,...) as a table header and the values in the cells, as in the example below.
How can I make table header with keys? Mine foreach
is as follows:
<table class="table table-hover table-striped">
<?php
foreach ($registros as $chave => $registro) {
echo "<tr>";
foreach ($registros[$chave] as $celula) {
echo "<td>" . $celula . "</td>";
}
echo "</tr>";
}
?>
</table>
If you make a
foreach($array as $key => $value
and print each'<td>'. $key .'</td>'
, n resolves?– andre_luiss
The value is called by the key:
$celula['indicador'];
, and an array of keys can be viaarray_keys($celula);
– Ivan Ferrer