3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function array2table($array, $recursive = false, $null = '&nbsp;') { // Sanity check if (empty($array) || !is_array($array)) { return false; } if (!isset($array[0]) || !is_array($array[0])) { $array = array($array); } // Start the table $table = "<table>\n"; // The header $table .= "\t<tr>"; // Take the keys from the first row as the headings foreach (array_keys($array[0]) as $heading) { $table .= '<th>' . $heading . '</th>'; } $table .= "</tr>\n"; // The body foreach ($array as $row) { $table .= "\t<tr>" ; foreach ($row as $cell) { $table .= '<td>'; // Cast objects if (is_object($cell)) { $cell = (array) $cell; } if ($recursive === true && is_array($cell) && !empty($cell)) { // Recursive mode $table .= "\n" . array2table($cell, true, true) . "\n"; } else { $table .= (strlen($cell) > 0) ? htmlspecialchars((string) $cell) : $null; } $table .= '</td>'; } $table .= "</tr>\n"; } $table .= '</table>'; return $table; } $obst = ['foo' => 'Apfel', 'bar' => 'Birne', 'baz' => [1,2,3]]; echo array2table($obst, true);
based on 5UUTY
Output for 5.4.0 - 7.1.0
<table> <tr><th>foo</th><th>bar</th><th>baz</th></tr> <tr><td>Apfel</td><td>Birne</td><td> <table> <tr><th>0</th><th>1</th><th>2</th></tr> <tr><td>1</td><td>2</td><td>3</td></tr> </table> </td></tr> </table>
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/eO887 on line 52
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/eO887 on line 52
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/eO887 on line 52
Process exited with code 255.