Bonjour,

Comment faire pour convertir une array php en table HTML ?
Dans mon cas, voici l'array:

array(4) {
  [0]=>
  array(5) {
    ["ip"]=>
    string(14) "77.xx.xx.188"
    ["port"]=>
    string(4) "2304"
    ["ping"]=>
    string(2) "16"
    ["guid"]=>
    string(32) "0e9fbffebd50f69759cfe732f7c4f374"
    ["name"]=>
    string(13) "Lucho Rodrigo"
  }
  [1]=>
  array(5) {
    ["ip"]=>
    string(13) "82.xx.xx.184"
    ["port"]=>
    string(4) "2304"
    ["ping"]=>
    string(2) "78"
    ["guid"]=>
    string(32) "29dceb0b616f47edb3e255e7df3059b5"
    ["name"]=>
    string(13) "Imran Zakhaev"
  }
  [3]=>
  array(5) {
    ["ip"]=>
    string(15) "109.xx.xx.236"
    ["port"]=>
    string(4) "2304"
    ["ping"]=>
    string(2) "78"
    ["guid"]=>
    string(32) "e4621b6d996a323023969a45815a505a"
    ["name"]=>
    string(11) "Bill Zelouf"
  }
  [4]=>
  array(5) {
    ["ip"]=>
    string(14) "88.xx.xx.237"
    ["port"]=>
    string(4) "2304"
    ["ping"]=>
    string(2) "32"
    ["guid"]=>
    string(32) "8f2db053c4892a0b10da377ad7e34930"
    ["name"]=>
    string(13) "Simon Ioannis"
  }
}

Et j'aimerais l'avoir sous cette forme :

<table>
    <thead>
        <tr>
            <th>Pseudo</th>
            <th>IP</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>name de l'array</td>
            <td>ip de l'array</td>
        </tr>
        <tr>
            <td>name de l'array 1</td>
            <td>ip de l'array 1</td>
        </tr>
        <tr>
            <td>name de l'array 2</td>
            <td>ip de l'array 2</td>
        </tr>
        <tr>
            <td>name de l'array 3</td>
            <td>ip de l'array 3</td>
        </tr>
        <tr>
            <td>name de l'array 4</td>
            <td>ip de l'array 4</td>
        </tr>
    </tbody>
</table>

2 réponses


Balbert
Réponse acceptée

Salut @Cobryn

Test ceci =>

function arrayToHTMLTable(array $items)
{
    $header = current($items);

    if (!is_array($header) || empty($header)) {
        throw new Exception("Impossible de générer l'entête.");
    }

    $table = '<table><thead><tr>';

    foreach ($header as $key => $val) {
            $table .= '<th>'  . $key . '</th>';
    }

    $table .= '</tr><thead><tbdoy>';

    foreach ($items as $item) {
        $table .= '<tr>';
        foreach ($header as $key => $val) {
            if (!isset($item[$key])) {
                throw new Exception("Un element du tableau ne contient pas la clé $key");
            } else {
                $table .= '<td>' .(string) $item[$key] . '<td>' ;
            }
        }
        $table .= '</tr>';
    }

    return $table . '</tbody></table>';    
}

Usage =>


$items = array(
    array(
        'ip' => '10.0.0.1',
        'port' => 80,
        'ping' => 16,
        'guid' => '0012'
    ),
    array(
        'ip' => '172.1.1.19',
        'port' => 8090,
        'ping' => 19,
        'guid' => '00223'
    )
);
echo arrayToHTMLTable($items);

avec une boucle, tu echo chaque ligne du tableau

$tab = ton array;
foreach ($tab as $var) {
    echo '<tr><th>'.var[0]['ip'].'</th><th>'.var[0]['port'] etc... '</tr>';
}