Lavoiesl\PhpBenchmark\Benchmark::outputTable PHP Method

outputTable() public static method

Output results in columns, padding right if values are string, left if numeric
public static outputTable ( array $lines, integer $padding = 3 )
$lines array array(array('Name' => 'Value'));
$padding integer space between columns
    public static function outputTable(array $lines, $padding = 3)
    {
        if (!$lines) {
            return;
        }
        $pad = function ($string, $width) use($padding) {
            if ($width > 0) {
                return str_pad($string, $width, " ") . str_repeat(' ', $padding);
            } else {
                return str_pad($string, -$width, " ", STR_PAD_LEFT) . str_repeat(' ', $padding);
            }
        };
        // init width with keys' length
        $cols = array_combine(array_keys($lines[0]), array_map('strlen', array_keys($lines[0])));
        foreach ($cols as $col => $width) {
            foreach ($lines as $line) {
                $width = max($width, strlen($line[$col]));
            }
            // pad left if numeric
            if (preg_match('/^[0-9]/', $line[$col])) {
                $width = -$width;
            }
            echo $pad($col, $width);
            $cols[$col] = $width;
        }
        echo "\n";
        foreach ($lines as $line) {
            foreach ($cols as $col => $width) {
                echo $pad($line[$col], $width);
            }
            echo "\n";
        }
    }