Convert PHP array to Bootstrap table

public function arrayToTable($rows, $idName = '', $settings = array()) {
    if (is_array($rows) && count($rows)) {
        $maxCellCounter = 0;
        foreach ($rows as $row) {
            $cellCounter = 0;
            foreach ($row as $col => $val) {
                $cellCounter++;
                if ($cellCounter >= $maxCellCounter) {
                    $maxCellCounter++;
                }
            }
        }
        $content .= '<table' . ($idName ? ' id="' . $idName . '"' : '') . ' class="table table-striped table-bordered tablesorter">';
        $content .= '<thead><tr>';
        if ($settings['keyNameAsHeadingTitle']) {
            $cellCounter = 0;
            foreach ($rows[0] as $colName => $colVal) {
                $colspan = '';
                if (count($rows[0]) == ($cellCounter + 1) && count($rows[0]) < ($maxCellCounter)) {
                    $colspan = ' colspan="' . ($maxCellCounter - ($cellCounter + 1)) . '"';
                }
                $content .= '<th' . $colspan . '>' . $colName . '</th>';
                $cellCounter++;
            }
        } else {
            $cellCounter = 0;
            foreach ($rows[0] as $colName => $colVal) {
                $colspan = '';
                if (count($rows[0]) == ($cellCounter + 1) && count($rows[0]) < ($maxCellCounter)) {
                    $colspan = ' colspan="' . ($maxCellCounter - ($cellCounter + 1)) . '"';
                }
                $content .= '<th' . $colspan . '>' . $colVal . '</th>';
                $cellCounter++;
            }
        }
        $content .= '</tr></thead><tbody>';
        $rowCounter = 0;
        if ($settings['keyNameAsHeadingTitle']) {
            $rowCounter = 1;
        }
        foreach ($rows as $row) {
            if ($rowCounter) {
                $trClass = array();
                if (is_array($settings['trClassClass']) && $settings['trClassClass'][($rowCounter + 1)]) {
                    $trClass = array();
                    $trClass[] = $settings['trClassClass'][($rowCounter + 1)];
                }
                $content .= '<tr' . (count($trClass) ? ' class="' . implode(' ', $trClass) . '"' : '') . '>';
                $cellCounter = 0;
                foreach ($row as $col => $val) {
                    $classes = array();
                    if (is_array($settings['cellClasses']) && isset($settings['cellClasses'][$cellCounter])) {
                        $classes[] = $settings['cellClasses'][$cellCounter];
                    }
                    $classes[] = 'cell' . ($cellCounter + 1);
                    $colspan = '';
                    if (count($row) == ($cellCounter + 1) && count($row) < ($maxCellCounter)) {
                        $colspan = ' colspan="' . ($maxCellCounter - ($cellCounter + 1)) . '"';
                    }
                    $content .= '<td' . (count($classes) ? ' class="' . implode(' ', $classes) . '"' : '') . $colspan . '>' . $val . '</td>';
                    $cellCounter++;
                }
                $content .= '</tr>';
            }
            $rowCounter++;
        }
        $content .= '</tbody>';
        if ($settings['sumTr']) {
            $GLOBALS['TSFE']->additionalHeaderData['tablesorter_js_' . $idName] = '<script data-ignore="true">
            jQuery(document).ready(function($) {
                    $('#' . $idName . '').tablesorter();
                    $('#' . $idName . '').sumtr({
                        readValue : function(e) {
                            return Math.round(e.html().toString().replace(/[^d.-]/g, '') * 100) / 100; return !isNaN(r) ? r : 0;
                        },
                        formatValue : function(val) { return Math.round(val*100)/100; }
                    });
                });
            </script>
            ';
            $content .= '
            <tfoot>
            <tr class="summary">
                <td class="text-right">Total:</td>
            ';
            $rowCounter = 0;
            foreach ($rows[0] as $colName => $colVal) {
                if ($rowCounter) {
                    $content .= '<td class="text-right grandTotal"></td>';
                }
                $rowCounter++;
            }
            $content .= '
            <tr>
            </tfoot>
            ';
        }
        $content .= '</table>';
        return $content;
    }
}