public function outputTable($data, $delimiter = '|')
    {
        $output = '';
        $firstRow = reset($data);
        $columnWidths = [];

        $columnCount = count($firstRow);
        $headerRow = array_keys($firstRow);

        if (!(is_integer(reset($headerRow)) && is_integer(end($headerRow)))) {
            // Add the header row to the data to detect the longest string
            array_unshift($data, $headerRow);
        } else {
            $headerRow = [];
        }

        // Collect the column widths
        for ($columnIndex = 0; $columnIndex < $columnCount; $columnIndex++) {
            $columnWidths[$columnIndex] = max(
                array_map(
                    function ($row) use ($columnIndex) {
                        $row = array_values($row);

                        return (isset($row[$columnIndex]) ? strlen($row[$columnIndex]) : 0);
                    },
                    $data
                )
            );
        }

        /*
         * Print the header if it is defined
         */
        if ($headerRow) {
            // Remove the header row to the data after detecting the longest string
            array_shift($data);

            // Add the header
            $output .= $delimiter . ' ';
            foreach ($headerRow as $columnIndex => $cell) {
                $output .= ''
                    . str_pad($cell, $columnWidths[$columnIndex])
                    . ' ' . $delimiter . ' ';
            }
            $output .= PHP_EOL;

            // Add the line below the header
            $output .= $delimiter . ' ';
            foreach ($headerRow as $columnIndex => $cell) {
                $output .= ''
                    . str_repeat('-', $columnWidths[$columnIndex])
                    . ' ' . $delimiter . ' ';
            }
            $output .= PHP_EOL;
        }

        /*
         * Print the table
         */
        foreach ($data as $row) {
            $row = array_values($row);

            $output .= $delimiter . ' ';
            foreach ($row as $columnIndex => $cell) {
                if (is_bool($cell)) {
                    $cell = $cell ? 'TRUE' : 'FALSE';
                }
                $output .= ''
                    . str_pad($cell, $columnWidths[$columnIndex])
                    . ' ' . $delimiter . ' ';
            }
            $output .= PHP_EOL;
        }
        $this->output($output);
    }
public function outputTable($data, $delimiter = '|')
    {
        $output = '';
        $firstRow = reset($data);
        $columnWidths = [];

        $columnCount = count($firstRow);
        $headerRow = array_keys($firstRow);

        if (!(is_integer(reset($headerRow)) && is_integer(end($headerRow)))) {
            // Add the header row to the data to detect the longest string
            array_unshift($data, $headerRow);
        } else {
            $headerRow = [];
        }

        // Collect the column widths
        for ($columnIndex = 0; $columnIndex < $columnCount; $columnIndex++) {
            $columnWidths[$columnIndex] = max(
                array_map(
                    function ($row) use ($columnIndex) {
                        $row = array_values($row);

                        return (isset($row[$columnIndex]) ? strlen($row[$columnIndex]) : 0);
                    },
                    $data
                )
            );
        }

        /*
         * Print the header if it is defined
         */
        if ($headerRow) {
            // Remove the header row to the data after detecting the longest string
            array_shift($data);

            // Add the header
            $output .= $delimiter . ' ';
            foreach ($headerRow as $columnIndex => $cell) {
                $output .= ''
                    . str_pad($cell, $columnWidths[$columnIndex])
                    . ' ' . $delimiter . ' ';
            }
            $output .= PHP_EOL;

            // Add the line below the header
            $output .= $delimiter . ' ';
            foreach ($headerRow as $columnIndex => $cell) {
                $output .= ''
                    . str_repeat('-', $columnWidths[$columnIndex])
                    . ' ' . $delimiter . ' ';
            }
            $output .= PHP_EOL;
        }

        /*
         * Print the table
         */
        foreach ($data as $row) {
            $row = array_values($row);

            $output .= $delimiter . ' ';
            foreach ($row as $columnIndex => $cell) {
                if (is_bool($cell)) {
                    $cell = $cell ? 'TRUE' : 'FALSE';
                }
                $output .= ''
                    . str_pad($cell, $columnWidths[$columnIndex])
                    . ' ' . $delimiter . ' ';
            }
            $output .= PHP_EOL;
        }
        $this->output($output);
    }
public function outputTable ( $ data , $ delimiter = '|' ) { $ output = '' ; $ firstRow = reset ( $ data ) ; $ columnWidths = [ ] ; $ columnCount = count ( $ firstRow ) ; $ headerRow = array_keys ( $ firstRow ) ; if ( ! ( is_integer ( reset ( $ headerRow ) ) && is_integer ( end ( $ headerRow ) ) ) ) { // Add the header row to the data to detect the longest string array_unshift ( $ data , $ headerRow ) ; } else { $ headerRow = [ ] ; } // Collect the column widths for ( $ columnIndex = 0 ; $ columnIndex < $ columnCount ; $ columnIndex ++ ) { $ columnWidths [ $ columnIndex ] = max ( array_map ( function ( $ row ) use ( $ columnIndex ) { $ row = array_values ( $ row ) ; return ( isset ( $ row [ $ columnIndex ] ) ? strlen ( $ row [ $ columnIndex ] ) : 0 ) ; } , $ data ) ) ; } /*
         * Print the header if it is defined
         */ if ( $ headerRow ) { // Remove the header row to the data after detecting the longest string array_shift ( $ data ) ; // Add the header $ output .= $ delimiter . ' ' ; foreach ( $ headerRow as $ columnIndex => $ cell ) { $ output .= '' . str_pad ( $ cell , $ columnWidths [ $ columnIndex ] ) . ' ' . $ delimiter . ' ' ; } $ output .= PHP_EOL ; // Add the line below the header $ output .= $ delimiter . ' ' ; foreach ( $ headerRow as $ columnIndex => $ cell ) { $ output .= '' . str_repeat ( '-' , $ columnWidths [ $ columnIndex ] ) . ' ' . $ delimiter . ' ' ; } $ output .= PHP_EOL ; } /*
         * Print the table
         */ foreach ( $ data as $ row ) { $ row = array_values ( $ row ) ; $ output .= $ delimiter . ' ' ; foreach ( $ row as $ columnIndex => $ cell ) { if ( is_bool ( $ cell ) ) { $ cell = $ cell ? 'TRUE' : 'FALSE' ; } $ output .= '' . str_pad ( $ cell , $ columnWidths [ $ columnIndex ] ) . ' ' . $ delimiter . ' ' ; } $ output .= PHP_EOL ; } $ this -> output ( $ output ) ; }
Prints the given data in a table

@param array  $data      Table data to display
@param string $delimiter Delimiter for table cells
Prints the given data in a table