Export array inside a table?

4

I need to export data from an array inside an html table, the function is working but I do not know how to export with while inside a table, I made an analogy with the code below.

function imprimir_Tbl($Id, $Nome){
$array = array();

for($i=0; $i<= $Id; $i++){
    $array["id"][] = $i + 1;
    $array["nome"][] = $Nome.' '.($i + 1);
}
return $array;}

Using var_dump the data is printed correctly, the expected result is as follows

<table border="0">
<thead>
    <tr>
        <th>Id</th>
        <th>Nome</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>1</td>
        <td>Nome 1</td>
    </tr>
    <tr>
        <td>2</td>
        <td>Nome 2</td>
    </tr>
    <tr>
        <td>3</td>
        <td>Nome 3</td>
    </tr>
    <tr>
        <td>4</td>
        <td>Nome 4</td>
    </tr>
</tbody>

    
asked by anonymous 10.01.2017 / 13:48

1 answer

3

You can use foreach to list results in HTML.

In the PHP function you can join the ID with the Name in the same Array, by using the variable $i of for .

PHP

<?php

function imprimir_Tbl($Id, $Nome){
    $array = array();

    for($i = 0; $i <= $Id; $i++){
        $array[$i]["id"]    = $i + 1;
        $array[$i]["nome"]  = $Nome.' '.($i + 1);
    }
    return $array;

}

$array = imprimir_Tbl(10, 'Nome');

HTML

<table border="0">
<thead>
    <tr>
        <th>Id</th>
        <th>Nome</th>
    </tr>
</thead>
<tbody>

<?php
   foreach($array as $key => $value){
?> 

<tr>
   <td><?=$value['id']?></td>
   <td><?=$value['nome']?></td>
</tr>

<?php   
   }
?>

</tbody>
    
10.01.2017 / 14:00