{"id":72,"date":"2007-08-02T23:50:10","date_gmt":"2007-08-03T03:50:10","guid":{"rendered":"http:\/\/www.virtualroadside.com\/blog\/index.php\/2007\/08\/02\/php-snippet-padded-table-on-cli\/"},"modified":"2007-08-02T23:52:25","modified_gmt":"2007-08-03T03:52:25","slug":"php-snippet-padded-table-on-cli","status":"publish","type":"post","link":"https:\/\/www.virtualroadside.com\/blog\/index.php\/2007\/08\/02\/php-snippet-padded-table-on-cli\/","title":{"rendered":"PHP Snippet: Padded table on CLI"},"content":{"rendered":"<p>While working on OWS, I created this neat little code snippet, which while it only took a few minutes to code, could be useful for someone just looking for a routine to display a simplistic table on the command line in PHP. Heres the code:<\/p>\n<pre>\r\n\/*\r\n\tPass this function an array of stuff and it displays a simple padded table. No borders.\r\n*\/<\/pre>\n<pre>\r\nfunction show_console_table($rows, $prepend = '', $header = true){\r\n\r\n\t$max = array();\r\n\r\n\t\/\/ find max first\r\n\tforeach ($rows as $r)\r\n\t\tfor ($i = 0;$i &lt; count($r);$i++)\r\n\t\t\t$max[$i] = max(array_key_exists($i,$max) ? $max[$i] : 0 ,strlen($r[$i]));\r\n\r\n\t\/\/ add a header?\r\n\tif ($header){\r\n\r\n\t\t\/\/ remove the first element\r\n\t\t$row = array_shift($rows);\r\n\r\n\t\techo \"$prepend\";\r\n\t\tfor ($i = 0;$i &lt; count($row);$i++)\r\n\t\t\techo str_pad($row[$i],$max[$i]) . \"  \";\r\n\t\techo \"\\\\n$prepend\" . str_repeat('=',array_sum($max) + count($max)*2) . \"\\\\n\";\r\n\r\n\t}\r\n\r\n\tforeach($rows as $row){\r\n\t\techo \"$prepend\";\r\n\t\tfor ($i = 0;$i &lt; count($row);$i++)\r\n\t\t\techo str_pad($row[$i],$max[$i]) . \"  \";\r\n\t\techo \"\\\\n\";\r\n\t}\r\n}<\/pre>\n<p>Like I said, pretty simple, but quite useful too. Just pass the function an array, and it outputs a  space-padded table with an optional header. Its probably been done already, but thats my implementation. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working on OWS, I created this neat little code snippet, which while it only took a few minutes to code, could be useful for someone just looking for a routine to display a simplistic table on the command line in PHP. Heres the code: \/* Pass this function an array of stuff and it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/72"}],"collection":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":0,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}