Skip to content

Instantly share code, notes, and snippets.

@jan-j
Last active August 8, 2018 07:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jan-j/6117215 to your computer and use it in GitHub Desktop.
Save jan-j/6117215 to your computer and use it in GitHub Desktop.
My solution to the task "Print an associative array as an ASCII table". More info can be found on task author page: http://phpixie.com/blog/test-tasks-for-php-interviews-that-developers-will-enjoy-solving/
<?php
const SPACING_X = 1;
const SPACING_Y = 0;
const JOINT_CHAR = '+';
const LINE_X_CHAR = '-';
const LINE_Y_CHAR = '|';
$table = 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'
),
);
function draw_table($table)
{
$nl = "\n";
$columns_headers = columns_headers($table);
$columns_lengths = columns_lengths($table, $columns_headers);
$row_separator = row_seperator($columns_lengths);
$row_spacer = row_spacer($columns_lengths);
$row_headers = row_headers($columns_headers, $columns_lengths);
echo '<pre>';
echo $row_separator . $nl;
echo str_repeat($row_spacer . $nl, SPACING_Y);
echo $row_headers . $nl;
echo str_repeat($row_spacer . $nl, SPACING_Y);
echo $row_separator . $nl;
echo str_repeat($row_spacer . $nl, SPACING_Y);
foreach ($table as $row_cells) {
$row_cells = row_cells($row_cells, $columns_headers, $columns_lengths);
echo $row_cells . $nl;
echo str_repeat($row_spacer . $nl, SPACING_Y);
}
echo $row_separator . $nl;
echo '</pre>';
}
function columns_headers($table)
{
return array_keys(reset($table));
}
function columns_lengths($table, $columns_headers)
{
$lengths = [];
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;
}
function row_seperator($columns_lengths)
{
$row = '';
foreach ($columns_lengths as $column_length) {
$row .= JOINT_CHAR . str_repeat(LINE_X_CHAR, (SPACING_X * 2) + $column_length);
}
$row .= JOINT_CHAR;
return $row;
}
function row_spacer($columns_lengths)
{
$row = '';
foreach ($columns_lengths as $column_length) {
$row .= LINE_Y_CHAR . str_repeat(' ', (SPACING_X * 2) + $column_length);
}
$row .= LINE_Y_CHAR;
return $row;
}
function row_headers($columns_headers, $columns_lengths)
{
$row = '';
foreach ($columns_headers as $header) {
$row .= LINE_Y_CHAR . str_pad($header, (SPACING_X * 2) + $columns_lengths[$header], ' ', STR_PAD_BOTH);
}
$row .= LINE_Y_CHAR;
return $row;
}
function row_cells($row_cells, $columns_headers, $columns_lengths)
{
$row = '';
foreach ($columns_headers as $header) {
$row .= LINE_Y_CHAR . str_repeat(' ', SPACING_X) . str_pad($row_cells[$header], SPACING_X + $columns_lengths[$header], ' ', STR_PAD_RIGHT);
}
$row .= LINE_Y_CHAR;
return $row;
}
draw_table($table);
/*
* Example output:
* +------------+-------+---------+---------+
* | Name | Color | Element | Likes |
* +------------+-------+---------+---------+
* | Trixie | Green | Earth | Flowers |
* | Tinkerbell | Blue | Air | Singing |
* | Blum | Pink | Water | Dancing |
* +------------+-------+---------+---------+
*/
@josecanciani
Copy link

Thanks, I just forked it and modify it a bit into a class for a little project of mine. I'm using for logging sql results to a log file.
Here's my fork: https://gist.github.com/josecanciani/ff535426bd5d453ef9c2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment