Forked from jan-j/Print an associative array as an ASCII table
Last active
July 18, 2017 15:29
-
-
Save josecanciani/ff535426bd5d453ef9c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Last version: https://gist.github.com/josecanciani/ff535426bd5d453ef9c2 | |
* | |
* Credits (adapted from): https://gist.github.com/doubleking/6117215 | |
* | |
**/ | |
namespace structure\util; | |
use \STR_PAD_BOTH; | |
class ArrayToAsciiTable { | |
const SPACING_X = 1; | |
const SPACING_Y = 0; | |
const JOINT_CHAR = '+'; | |
const LINE_X_CHAR = '-'; | |
const LINE_Y_CHAR = '|'; | |
function draw_table($table){ | |
if (!$table) { | |
$table = array(array('' => 'No data found')); | |
} | |
$nl = "\n"; | |
$columns_headers = $this->columns_headers($table); | |
$columns_lengths = $this->columns_lengths($table, $columns_headers); | |
$row_separator = $this->row_seperator($columns_lengths); | |
$row_spacer = $this->row_spacer($columns_lengths); | |
$row_headers = $this->row_headers($columns_headers, $columns_lengths); | |
$ascii = $row_separator . $nl; | |
$ascii .= str_repeat($row_spacer . $nl, self::SPACING_Y); | |
$ascii .= $row_headers . $nl; | |
$ascii .= str_repeat($row_spacer . $nl, self::SPACING_Y); | |
$ascii .= $row_separator . $nl; | |
$ascii .= str_repeat($row_spacer . $nl, self::SPACING_Y); | |
foreach($table as $row_cells) | |
{ | |
$row_cells = $this->row_cells($row_cells, $columns_headers, $columns_lengths); | |
$ascii .= $row_cells . $nl; | |
$ascii .= str_repeat($row_spacer . $nl, self::SPACING_Y); | |
} | |
$ascii .= $row_separator; | |
return $ascii; | |
} | |
private function columns_headers($table){ | |
return array_keys(reset($table)); | |
} | |
private function columns_lengths($table, $columns_headers){ | |
$lengths = array(); | |
foreach($columns_headers as $header) | |
{ | |
$header_length = strlen($header); | |
$max = $header_length; | |
foreach($table as $row) | |
{ | |
$length = strlen($row[$header]); | |
if($length > $max) | |
$max = $length; | |
} | |
if(($max % 2) != ($header_length % 2)) | |
$max += 1; | |
$lengths[$header] = $max; | |
} | |
return $lengths; | |
} | |
private function row_seperator($columns_lengths){ | |
$row = ''; | |
foreach($columns_lengths as $column_length) | |
{ | |
$row .= self::JOINT_CHAR . str_repeat(self::LINE_X_CHAR, (self::SPACING_X * 2) + $column_length); | |
} | |
$row .= self::JOINT_CHAR; | |
return $row; | |
} | |
private function row_spacer($columns_lengths){ | |
$row = ''; | |
foreach($columns_lengths as $column_length) | |
{ | |
$row .= self::LINE_Y_CHAR . str_repeat(' ', (self::SPACING_X * 2) + $column_length); | |
} | |
$row .= self::LINE_Y_CHAR; | |
return $row; | |
} | |
private function row_headers($columns_headers, $columns_lengths){ | |
$row = ''; | |
foreach($columns_headers as $header) | |
{ | |
$row .= self::LINE_Y_CHAR . str_pad($header, (self::SPACING_X * 2) + $columns_lengths[$header], ' ', STR_PAD_BOTH); | |
} | |
$row .= self::LINE_Y_CHAR; | |
return $row; | |
} | |
private function row_cells($row_cells, $columns_headers, $columns_lengths){ | |
$row = ''; | |
foreach($columns_headers as $header) | |
{ | |
$row .= self::LINE_Y_CHAR . str_repeat(' ', self::SPACING_X) . str_pad($row_cells[$header], self::SPACING_X + $columns_lengths[$header], ' ', STR_PAD_RIGHT); | |
} | |
$row .= self::LINE_Y_CHAR; | |
return $row; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment