public static array2table ( array $array, boolean $transpose = false, boolean $recursive = false, boolean $typeHint = true, array $tableOptions = ['class' => 'table table-bordered table-striped'], array $keyOptions = [], array $valueOptions = ['style' => 'cursor: default; border-bottom: 1px #aaa dashed;'], string $null = '<span class="not-set">(not set)</span>' ) : string | boolean | ||
$array | array | the associative array to be converted |
$transpose | boolean | whether to show keys as rows instead of columns. This parameter should be used only for a single dimensional associative array. If used for a multidimensional array, the sub array will be imploded as text. |
$recursive | boolean | whether to recursively generate tables for multi-dimensional arrays |
$typeHint | boolean | whether to show the data type as a hint |
$tableOptions | array | the HTML attributes for the table |
$keyOptions | array | the HTML attributes for the array key |
$valueOptions | array | the HTML attributes for the array value |
$null | string | the content to display for blank cells |
return | string | boolean |
public static function array2table($array, $transpose = false, $recursive = false, $typeHint = true, $tableOptions = ['class' => 'table table-bordered table-striped'], $keyOptions = [], $valueOptions = ['style' => 'cursor: default; border-bottom: 1px #aaa dashed;'], $null = '<span class="not-set">(not set)</span>')
{
// Sanity check
if (empty($array) || !is_array($array)) {
return false;
}
// Start the table
$table = Html::beginTag('table', $tableOptions) . "\n";
// The header
$table .= "\t<tr>";
if ($transpose) {
foreach ($array as $key => $value) {
if ($typeHint) {
$valueOptions['title'] = self::getType(strtoupper($value));
}
if (is_array($value)) {
$value = '<pre>' . print_r($value, true) . '</pre>';
} else {
$value = Html::tag('span', $value, $valueOptions);
}
$table .= "\t\t<th>" . Html::tag('span', $key, $keyOptions) . "</th>" . "<td>" . $value . "</td>\n\t</tr>\n";
}
$table .= "</table>";
return $table;
}
if (!isset($array[0]) || !is_array($array[0])) {
$array = array($array);
}
// Take the keys from the first row as the headings
foreach (array_keys($array[0]) as $heading) {
$table .= '<th>' . Html::tag('span', $heading, $keyOptions) . '</th>';
}
$table .= "</tr>\n";
// The body
foreach ($array as $row) {
$table .= "\t<tr>";
foreach ($row as $cell) {
$table .= '<td>';
// Cast objects
if (is_object($cell)) {
$cell = (array) $cell;
}
if ($recursive === true && is_array($cell) && !empty($cell)) {
// Recursive mode
$table .= "\n" . static::array2table($cell, true, true) . "\n";
} else {
if (!is_null($cell) && is_bool($cell)) {
$val = $cell ? 'true' : 'false';
$type = 'boolean';
} else {
$chk = strlen($cell) > 0;
$type = $chk ? self::getType($cell) : 'NULL';
$val = $chk ? htmlspecialchars((string) $cell) : $null;
}
if ($typeHint) {
$valueOptions['title'] = $type;
}
$table .= Html::tag('span', $val, $valueOptions);
}
$table .= '</td>';
}
$table .= "</tr>\n";
}
$table .= '</table>';
return $table;
}