How can I build an HTML Table with the content of Arrays?

0

Hi, I'm trying to make a function () php in which to enter an array and output a variable that contains an HTML table with the content of the Arrays

[ARRAY]

Array
(
    [0] => Array
        (
            [tipo] =>  Ahorros
            [code] =>  0730151719
            [simbolo] =>  $
            [total] =>  22.468
        )

    [1] => Array
        (
            [tipo] =>  Sueldo
            [code] =>  1511981973
            [simbolo] =>  U$S
            [total] =>  689,44
        )

    [2] => Array
        (
            [tipo] =>  Gastos
            [code] =>  1520131948
            [simbolo] =>  $
            [total] =>  685.965,81
        )

    [3] => Array
        (
            [tipo] =>  Servicios
            [cuenta] =>  1940334931
            [simbolo] =>  U$S
            [total] =>  38.237,83
        )

)

[PHP]

  

I made the attempt but it does not nest well

asked by Paolo Dev 25.05.2018 в 06:02
source

1 answer

0
function formateHTML($array){
    $title = ''; $tab = '';
    foreach ($array as $i => $tr) {
        $tab.= '<tr>';
        foreach ($tr as $key => $value) {
            if($i==0) $title.= '<th scope="col">'.$key.'</td>';
            $tab.= '<td>'.$value.'</td>';
        }
        $tab.= '</tr>';
    }
    $table = '<table border="1">';
    $table.= '<tr>'.$title.'</tr>';
    $table.= $tab;
    $table.= '</table>';
    return $table;
}
    
answered by 25.05.2018 / 06:58
source