Skip to content

Instantly share code, notes, and snippets.

@maulikvora
Created October 3, 2014 07:30
Show Gist options
  • Save maulikvora/930a3e99192bdf0275d0 to your computer and use it in GitHub Desktop.
Save maulikvora/930a3e99192bdf0275d0 to your computer and use it in GitHub Desktop.
Print an associative array as an ASCII table and also apply different colors to each column in the table
<?php
$initial_array = array(
array(
'Name' => 'Trixie',
'Color' => 'Green',
'Element' => 'Earth',
'Likes' => 'Flowers'
),
array(
'Name' => 'Tinkerbell',
'Element' => 'Air',
'Likes' => 'Singing',
'Color' => 'Blue'
),
array(
'Element' => 'Water',
'Likes' => 'Dancing',
'Name' => 'Blum',
'Color' => 'Pink'
),
);
$column_colors = array('#00FFFF', 'pink','#00FF88','#99BBFF');
const horizontal_space = 1;
const vertical_space = 0;
const plus_char = '+';
const dash_char = '-';
const pipe_char = '|';
function render_ascii_table($initial_array, $column_colors){
if(!empty($initial_array) && (count($initial_array[0]) != count($column_colors) ))
die('color codes doesn\'t match with key numbers');
$nl = "\n";
$headers = get_table_headers($initial_array);
$data_lengths = data_lengths($initial_array, $headers);
$row_separator = separate_row($data_lengths, $column_colors);
$row_space = add_row_space($data_lengths, $column_colors);
$row_headers = display_header($headers, $data_lengths, $column_colors);
echo '<pre>';
echo $row_separator . $nl;
echo str_repeat($row_space . $nl, vertical_space);
echo $row_headers . $nl;
echo str_repeat($row_space . $nl, vertical_space);
echo $row_separator . $nl;
echo str_repeat($row_space . $nl, vertical_space);
foreach($initial_array as $row_data)
{
$row_data = display_data($row_data, $headers, $data_lengths, $column_colors);
echo $row_data . $nl;
echo str_repeat($row_space . $nl, vertical_space);
}
echo $row_separator . $nl;
echo '</pre>';
}
function get_table_headers($initial_array){
return array_keys(reset($initial_array));
}
function data_lengths($initial_array, $headers){
$lengths = array();
foreach($headers as $header)
{
$header_length = strlen($header);
$max = $header_length;
foreach($initial_array as $row)
{
$length = strlen($row[$header]);
if($length > $max)
$max = $length;
}
if(($max % 2) != ($header_length % 2))
$max += 1;
$lengths[$header] = $max;
}
return $lengths;
}
function separate_row($data_lengths, $column_colors){
$row = '';
$color_index = 0;
foreach($data_lengths as $column_length)
{
$row .= plus_char .'<span style="background-color:'.$column_colors[$color_index].'">'. str_repeat(dash_char, (horizontal_space * 2) + $column_length).'</span>';
$color_index++;
}
$row .= plus_char;
return $row;
}
function add_row_space($data_lengths, $column_colors){
$row = '';
$color_index = 0;
foreach($data_lengths as $column_length)
{
$row .= pipe_char .'<span style="background-color:'.$column_colors[$color_index].'">'. str_repeat(' ', (horizontal_space * 2) + $column_length).'</span>';
$color_index++;
}
$row .= pipe_char;
return $row;
}
function display_header($headers, $data_lengths, $column_colors){
$row = '';
$color_index = 0;
foreach($headers as $header)
{
$row .= pipe_char . '<span style="background-color:'.$column_colors[$color_index].'" >' . str_pad($header, (horizontal_space * 2) + $data_lengths[$header], ' ', STR_PAD_BOTH).'</span>';
$color_index++;
}
$row .= pipe_char;
return $row;
}
function display_data($row_data, $headers, $data_lengths, $column_colors){
$row = '';
$color_index = 0;
foreach($headers as $header)
{
$row .= pipe_char . '<span style="background-color:'.$column_colors[$color_index].'" >' . str_repeat(' ', horizontal_space) . str_pad($row_data[$header], horizontal_space + $data_lengths[$header], ' ', STR_PAD_RIGHT).'</span>';
$color_index++;
}
$row .= pipe_char;
return $row;
}
render_ascii_table($initial_array, $column_colors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment