-
-
Save theStrangeAdventurer/526db8baf0e96551fbe18b37bc4f77dc to your computer and use it in GitHub Desktop.
print ASCII table from two-dimensional array
This file contains 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 | |
$input = [ | |
'Title1' => [ | |
'item1_col1', | |
'item2_colddfs1', | |
'item3_col1', | |
'item4_cddol1' | |
], | |
'LargeTitle2' => [ | |
'item1_l2', | |
'item2_col2', | |
'item3_ol2', | |
'item4_colarge_2', | |
'item5_col2' | |
], | |
'Titl3' => [ | |
'item1_col3', | |
'item2_col3eeewrwrwr', | |
'item3_col3' | |
] | |
]; | |
$space_x = 0; | |
$space_y = 1; | |
$joint = "+"; | |
$line_x = "-"; | |
$line_y = "|"; | |
$nl = "\n"; | |
/** | |
* function return array of headers lengths | |
* | |
* @param array $headers | |
* @return array | |
*/ | |
function get_headers_length($headers) { | |
$headers_lengths = []; | |
foreach ($headers as $header) { | |
$headers_lengths[] = strlen($header); | |
} | |
return $headers_lengths; | |
} | |
/** | |
* function return array of final lengths of columns | |
* | |
* @param array $input | |
* @param array $headers_results | |
* @return array | |
*/ | |
function get_results_lengths($input, $headers_results) { | |
$results_lenghts = []; | |
foreach ($input as $head => $row) { | |
$max_row_length = $headers_results[$head]; | |
foreach ($row as $cell) { | |
if($max_row_length < strlen($cell)) $max_row_length = strlen($cell); | |
} | |
$results_lenghts[]=$max_row_length; | |
} | |
return $results_lenghts; | |
} | |
/** | |
* function return row separator for rows of table (+-------+------+) | |
* | |
* @param array $results_lenghts | |
* @param string $joint '+' | |
* @param string $line_x '-' | |
* @param integer $space_x | |
* @param string $nl '\n' | |
* @return string | |
*/ | |
function row_separator($results_lenghts, $joint, $line_x, $space_x, $nl) { | |
$row = ''; | |
foreach ($results_lenghts as $length){ | |
$row .= $joint . str_repeat($line_x, ($space_x*2) + $length); | |
} | |
$row .= $joint . $nl; | |
return $row; | |
} | |
/** | |
* function return headers_row of table with values | |
* | |
* @param type $headers | |
* @param type $results_lenghts | |
* @param integer $space_x | |
* @param type $line_y | |
* @param string $nl '\n' | |
* @param string $cell cell value | |
* @return string | |
*/ | |
function row($headers, $results_lenghts, $space_x,$line_y, $nl, $cell = NULL) { | |
// TODO переделать эту ф-ю, дублирование кода в data_rows() | |
$headers_row = ''; | |
for($i = 0; $i < count($headers); $i++) { | |
$current_cell = $cell ? $cell : $headers[$i]; | |
$headers_row .= $line_y . str_repeat(" ", $space_x*2) | |
. str_pad($current_cell,$results_lenghts[$i], " ",STR_PAD_BOTH ) | |
. str_repeat(" ", $space_x*2); | |
} | |
$headers_row .= $line_y . $nl; | |
return $headers_row; | |
} | |
/** | |
* function return array with rows (with values) for table without headers_row | |
* | |
* @param array $input | |
* @param array $headers | |
* @param array $results_lenghts | |
* @param string $joint '+' | |
* @param string $line_x '-' | |
* @param integer $space_x | |
* @param string $line_y '|' | |
* @param string $nl '\n' | |
* @return array | |
*/ | |
function data_rows($input, $headers, $results_lenghts, $joint, $line_x, $space_x, $line_y, $nl) { | |
$columns_lenght = array_combine($headers, $results_lenghts); | |
$data = []; | |
$max_values = 0; | |
foreach ($input as $title => $values) { | |
if($max_values < count($values)) $max_values = count($values); | |
} | |
for($i = 0 ; $i < $max_values; $i++) { | |
$row = ""; | |
foreach ($headers as $header) { | |
if(isset($input[$header][$i])) { | |
$cell = $input[$header][$i]; | |
}else { | |
$cell = ''; | |
} | |
$string = $line_y . str_repeat(" ", $space_x*2) | |
. str_pad($cell, $columns_lenght[$header], " ",STR_PAD_BOTH ) | |
. str_repeat(" ", $space_x*2); | |
$row .= $string ; | |
} | |
$data []= $row . $line_y . $nl; | |
} | |
return $data; | |
} | |
/** | |
* | |
* @param array $input | |
* @param array $headers | |
* @param array $results_lenghts | |
* @param string $joint | |
* @param string $line_x '-' | |
* @param integer $space_x | |
* @param string $line_y '|' | |
* @param string $nl '\n' | |
*/ | |
function draw_table ($input, $headers, $results_lenghts, $joint, $line_x, $space_x, $line_y, $nl) { | |
$headers_row = row($headers, $results_lenghts, $line_x, $line_y, $nl); | |
$separator = row_separator($results_lenghts, $joint, $line_x, $space_x, $nl); | |
$data = data_rows($input, $headers, $results_lenghts, $joint, $line_x, $space_x, $line_y, $nl); | |
print_r($separator); | |
print_r($headers_row); | |
print_r($separator); | |
for($i = 0; $i < count($data); $i++) { | |
print_r($data[$i]); | |
print_r($separator); | |
} | |
} | |
// get headers of table | |
$headers = array_keys($input); | |
// get length of headers | |
$headers_lengths = get_headers_length($headers); | |
// create assoc array of headers and then lengths | |
$headers_results = array_combine($headers, $headers_lengths); | |
// get results lengths of columns | |
$results_lenghts = get_results_lengths($input, $headers_results); | |
echo '<pre>'; | |
draw_table($input, $headers, $results_lenghts, $joint, $line_x, $space_x, $line_y, $nl); | |
echo '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this script will be print table like this