Skip to content

Instantly share code, notes, and snippets.

@ihabunek
Created September 7, 2012 09:43
Show Gist options
  • Save ihabunek/3664703 to your computer and use it in GitHub Desktop.
Save ihabunek/3664703 to your computer and use it in GitHub Desktop.
table_dump(): One of my favourite PHP debug functions.
<?php
/** Number of spaces to leave between columns. */
define('TABLE_DUMP_COLUMN_PADDING', 3);
/** Maximum number of chars in a column. Longer values will be trucnated. */
define('TABLE_DUMP_COLUMN_MAX_LENGTH', 50);
/**
* Takes an array of arrays, such as table data from a database and
* prints it nicely. For example:
*
* tax_id caption_id valid_from valid_to percentage
* ============================================================
* 5 tax.pdv.full 2009-07-31 22.000000
* 6 tax.pdv.full 2009-08-01 2012-02-29 23.000000
* 7 tax.pdv.full 2012-03-01 25.000000
* 8 tax.zero 0.000000
*
*
* If using non-ascii characters, make sure to set mb_internal_encoding
* to the appropreiate value, e.g.:
* <pre>mb_internal_encoding('UTF-8');</pre>
*
* @param array $array Input array.
* @param array $return If set to true, the dump will be returned as string
* instead of printing it.
*/
function table_dump($array, $return = false)
{
if (!is_array($array))
{
trigger_error("Invalid input for table dump. Not an array.", E_USER_WARNING);
return false;
}
if (empty($array))
{
trigger_error("Invalid input for table dump. Empty array.", E_USER_WARNING);
return false;
}
// The output buffer
$output = "";
// Check input data
foreach($array as &$row)
{
// Convert classes to rows
if ($row instanceof stdClass)
$row = (array) $row;
if (!is_array($row))
{
trigger_error("Invalid input for table dump. Not an array of arrays.", E_USER_WARNING);
return false;
}
}
unset($row);
$lengths = array();
$titles = array();
// Get titles from first row keys
$titles = array_keys(reset($array));
// Trim and record title lengths
foreach($titles as $key => &$value)
{
$value = trim($value);
$lengths[$key] = mb_strlen($value);
}
unset($value);
// Process each row
foreach($array as &$row)
{
$key = 0;
// Record data lengths
foreach($row as &$value)
{
$value = __table_dump_get_val($value);
if (!isset($lengths[$key]))
$lengths[$key] = 0;
if (mb_strlen($value) > $lengths[$key])
$lengths[$key] = mb_strlen($value);
$key++;
}
}
unset($row, $value);
// Original solution used vsprintf() instead of padding manually, but that
// function does not work well with unicode.
// Determine total row length
$totalLength = 0;
foreach($lengths as $len)
{
$totalLength += $len;
}
// Account for padding between columns
$totalLength += TABLE_DUMP_COLUMN_PADDING * (count($lengths) - 1);
// Print the titles
foreach($titles as $key => $value)
{
$output .= __table_dump_strpad($value, $lengths[$key]);
$output .= str_repeat(" ", TABLE_DUMP_COLUMN_PADDING); // Padding
}
$output .= PHP_EOL;
// Print the line under titles
$output .= str_repeat("=", $totalLength) . PHP_EOL;
// Print the rows
foreach($array as $row)
{
foreach(array_values($row) as $key => $value)
{
$output .= __table_dump_strpad($value, $lengths[$key]);
$output .= str_repeat(" ", TABLE_DUMP_COLUMN_PADDING); // Padding
}
$output .= PHP_EOL;
}
if ($return) {
return $output;
}
echo $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment