Last active
December 27, 2015 21:49
-
-
Save christoferd/7394561 to your computer and use it in GitHub Desktop.
Create plain Text table from Array, or Array of Arrays. Great for dumping database table data to text only logs or display. Supports vertical single record display, and multi row (array of arrays) display. - 11-Nov-2013 Added Hanlde Empty 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
/** | |
* Create plain Text table from Array, or Array of Arrays. | |
* Great for dumping database table data to text only logs or display. | |
* @use String::textTableFromArray($myArrayOfArrays,'My Multi Row Table',true); // multiple records/array | |
* ArrayHelper::textTableFromArray($simpleKeyValArray,'A Single Record',false); // single row, vertical view | |
* @requires String::humanize() ... or just remove/override it. | |
* | |
* @param $arr | |
* @param string $tableTitle | |
* @param bool $horizontalDisplay | |
* | |
* @return string | |
*/ | |
class ArrayHelper { | |
public static function textTableFromArray($arr, $tableTitle = '', $horizontalDisplay = false) { | |
$text = ''; | |
$newLine = "\r\n"; | |
$longestKey = 0; | |
$longestVal = 0; | |
$fieldNames = array(); | |
$fieldLen = array(); | |
// Hanlde Empty Array | |
if(empty($arr)) | |
{ | |
$arr = array(''=>'Empty'); | |
} | |
if($horizontalDisplay) { | |
if(isset($arr[0])) { | |
$rows = $arr; | |
} | |
else { | |
$rows = array($arr); | |
} | |
} | |
else { | |
$rows = array($arr); | |
} | |
foreach($rows as $row) | |
{ | |
// fix | |
if(isset($row[0])) { | |
$row = $row[0]; | |
} | |
foreach($row as $key => $val) | |
{ | |
if(strlen($val) > $longestVal) { | |
$longestVal = strlen($val); | |
} | |
if(strlen($key) > $longestKey) { | |
$longestKey = strlen($key); | |
} | |
// Horizontal stuff | |
if(empty($fieldLen[$key])) | |
{ | |
// At least width of KEY | |
$fieldLen[$key] = strlen($key); | |
} | |
if($fieldLen[$key] < strlen($val)) | |
{ | |
$fieldLen[$key] = strlen($val); | |
} | |
// - horizontal stuff | |
} | |
$fieldNames = array_keys($row); | |
} | |
if($horizontalDisplay) | |
{ | |
// Horizontal | |
$sum = array_sum($fieldLen); | |
$totalLen = (count($fieldLen)*3) + $sum; | |
$tableLine = sprintf("|%'-{$totalLen}s|",'').$newLine; | |
// Table Title | |
if($tableTitle != '') { | |
$text .= $tableLine; | |
$text .= sprintf("|%-{$totalLen}s|",' '.$tableTitle).$newLine; | |
} | |
$text .= $tableLine; | |
// Titles | |
foreach($fieldNames as $fieldName) | |
{ | |
$len = $fieldLen[$fieldName]; | |
$text .= sprintf("| %-{$len}s ", String::humanize($fieldName)); | |
} | |
$text .= ' |'.$newLine; | |
$text .= $tableLine; | |
// Rows | |
foreach($rows as $row) | |
{ | |
// fix | |
if(isset($row[0])) { | |
$row = $row[0]; | |
} | |
foreach($row as $key => $val) | |
{ | |
$len = $fieldLen[$key]; | |
$text .= sprintf("| %-{$len}s ", $val); | |
} | |
$text .= ' |'.$newLine; | |
} | |
$text .= $tableLine; | |
} | |
else | |
{ | |
// Vertical | |
$tableLine = sprintf("| %'-{$longestKey}s | %'-{$longestVal}s |",'','').$newLine; | |
if($tableTitle != '') { | |
$text .= $tableLine; | |
$text .= sprintf("| %-{$longestKey}s %-{$longestVal}s |",$tableTitle,'').$newLine; | |
} | |
$text .= $tableLine; | |
foreach($arr as $key => $val) | |
{ | |
$text .= sprintf("| %-{$longestKey}s | %-{$longestVal}s |",$key,strval($val)).$newLine; | |
} | |
$text .= $tableLine; | |
} | |
return $newLine | |
.$text.$newLine; | |
} // end function | |
} // end class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I struggled to easily find a way to output mysql results in a plain text format so I created this. Hopefully other people can find it via GitHub search or Google ; )