3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $input = [ [ "forename" => "Dave", "surname" => "Wood", "dob" => "23/05/1986", "gender" => "Male", ], ]; function outputr(array $input) { // work out the max length for each field $lengths = []; foreach ($input as $row) { foreach ($row as $key => $value) { if (!isset($lengths[$key])) { $lengths[$key] = 0; } $kl = strlen($key); $vl = strlen($value); $ln = ($kl > $vl) ? $kl : $vl; if ($ln > $lengths[$key]) { $lengths[$key] = $ln; } } } // output the table header foreach ($lengths as $key => $length) { echo "+" . str_repeat("-", $length); } echo "+\n"; foreach ($lengths as $key => $length) { $pad = $length - strlen($key); echo "| {$key}"; echo str_repeat(" ", $length); } echo "|\n"; } outputr($input);
Output for 5.6.0 - 5.6.30, hhvm-3.18.5 - 3.22.0, 7.1.0 - 7.3.0beta1
+--------+-------+----------+------+ | forename | surname | dob | gender |